Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

How to measure or assign Movement Speed ?

Discussion in '2D' started by kutmstudios, Aug 9, 2018.

  1. kutmstudios

    kutmstudios

    Joined:
    Aug 9, 2018
    Posts:
    5
    Hi guys,

    I am working on a 2D Game and I would like to measure or assign a Movement Speed for the character, any ideas how can I get this done?

    My goal is to have a value that represent the speed of the character and be able to modify the value depending on the character.

    Here is the code i am using:

    Code (CSharp):
    1. public class Player_Movement : MonoBehaviour
    2. {
    3.     private BoxCollider2D boxCollider;
    4.     private Vector3 moveDelta;
    5.     private RaycastHit2D hit;
    6.     // Test Speed
    7.     public float MoveSpeed = 3.0f;
    8.     private void Start()
    9.     {
    10.         boxCollider = GetComponent<BoxCollider2D>();
    11.     }
    12.     private void FixedUpdate()
    13.     {
    14.         float x = Input.GetAxisRaw("Horizontal");
    15.         float y = Input.GetAxisRaw("Vertical");
    16.      
    17.         // Reset MoveDelta
    18.         moveDelta = new Vector3(x,y,0);
    19.         // Swap sprite direction, wether you are going right or left
    20.         if (moveDelta.x < 0)
    21.             transform.localScale = Vector3.one;
    22.         else if (moveDelta.x > 0)
    23.             transform.localScale = new Vector3(-1, 1, 1);
    24.         // Make sure you can move in this direction by casting a box there first, if the box returns null we are free to move
    25.         hit = Physics2D.BoxCast(transform.position,boxCollider.size, 0, new Vector2(0, moveDelta.y), Mathf.Abs(moveDelta.y * Time.deltaTime), LayerMask.GetMask("Player","Blocking"));
    26.         if(hit.collider == null)
    27.         {
    28.             // Make this thing move
    29.             transform.Translate(0, moveDelta.y * Time.deltaTime, 0);
    30.         }
    31.         hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(moveDelta.x, 0), Mathf.Abs(moveDelta.x * Time.deltaTime), LayerMask.GetMask("Player", "Blocking"));
    32.         if (hit.collider == null)
    33.         {
    34.             // Make this thing move
    35.             transform.Translate(moveDelta.x * Time.deltaTime, 0, 0);
    36.         }
     
    Last edited: Aug 15, 2018
  2. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    First, please use [ code = CSharp ] tags above to make it more readable.

    Second, It looks like your movement per frame is the moveDelta vector multiplied by time.deltaTime. Whenever you multiply by Time.deltaTime you are adjusting for the per-frame amount. So that means that in 1 second, it will move by the amount of moveDelta, hence moveDelta is your speed per second. So if you want to adjust this based on a value that is adjustable per character, just add a public float and multiply your moveDelta by it.
    public float maxSpeed;
    ...
    transform.Translate (moveDetla.x * maxSpeed * Time.deltaTime, 0, 0)
     
  3. kutmstudios

    kutmstudios

    Joined:
    Aug 9, 2018
    Posts:
    5
    Thanks Barskey, it did work out!