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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. 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)
     
    ERVIN8 likes this.
  3. kutmstudios

    kutmstudios

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