Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

moving a sprite

Discussion in '2D' started by TheSaltySeaFood, Mar 19, 2020.

  1. TheSaltySeaFood

    TheSaltySeaFood

    Joined:
    Mar 19, 2020
    Posts:
    4
    So I have a .PNG file of a ship and I want to move it left right up down from a top down screen. I followed a brackey tutorial on how to move top down characters but it isn't working for some reason. If someone could look at my code/ set up and see what I'm doing wrong that would be great. (Don't judge the scene I'm a beginner and just want to see things move)




    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class player_movement : MonoBehaviour
    6. {
    7.     public float moveSpeed = 5f;
    8.  
    9.     public Rigidbody2D rb;
    10.  
    11.     Vector2 movement;
    12.  
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.  
    18.         // Input
    19.        movement.x = Input.GetAxisRaw("Horizantal");
    20.        movement.y = Input.GetAxisRaw("Vertical");
    21.     }
    22.  
    23.  
    24.     void FixedUpdate()
    25.     {
    26.  
    27.         //Movement
    28.         rb.MovePosition(rb.position + movement *moveSpeed* Time.fixedDeltaTime);
    29.  
    30.     }
    31. }
    32.  
     
  2. TheSaltySeaFood

    TheSaltySeaFood

    Joined:
    Mar 19, 2020
    Posts:
    4
    sorry here's the image
     

    Attached Files: