Search Unity

How do you specify separate horizontal and vertical movement speeds?

Discussion in 'Scripting' started by Khalirei, Mar 21, 2019.

  1. Khalirei

    Khalirei

    Joined:
    Mar 12, 2019
    Posts:
    2
    So I'm looking at the project example for the "Isometric 2D Environments with Tilemap" blog and when I tested the example character, it didn't move along the isometric axis correctly. This can be fixed by simply specifying a faster horizontal movement speed while keeping the vertical at default. Thing is, I don't know anything about creating new code or adding new lines of code to existing scripts. The best I can do is edit existing lines of code at the moment, it's a slow learning process for me.

    The character uses two scripts, IsometricPlayerMovementController.cs and IsometricCharacterRenderer.cs.

    I tried looking up tutorials and threads about this, but I haven't found a single thing. If anyone can help me out I'd greatly appreciate it.

    IsometricPlayerMovementController.cs:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class IsometricPlayerMovementController : MonoBehaviour
    6. {
    7.  
    8.     public float movementSpeed = 1f;
    9.     IsometricCharacterRenderer isoRenderer;
    10.  
    11.     Rigidbody2D rbody;
    12.  
    13.     private void Awake()
    14.     {
    15.         rbody = GetComponent<Rigidbody2D>();
    16.         isoRenderer = GetComponentInChildren<IsometricCharacterRenderer>();
    17.     }
    18.  
    19.  
    20.     // Update is called once per frame
    21.     void FixedUpdate()
    22.     {
    23.         Vector2 currentPos = rbody.position;
    24.         float horizontalInput = Input.GetAxis("Horizontal");
    25.         float verticalInput = Input.GetAxis("Vertical");
    26.         Vector2 inputVector = new Vector2(horizontalInput, verticalInput);
    27.         inputVector = Vector2.ClampMagnitude(inputVector, 1);
    28.         Vector2 movement = inputVector * movementSpeed;
    29.         Vector2 newPos = currentPos + movement * Time.fixedDeltaTime;
    30.         isoRenderer.SetDirection(movement);
    31.         rbody.MovePosition(newPos);
    32.     }
    33. }
    34.  
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    multiply on each axis separately.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class IsometricPlayerMovementController : MonoBehaviour
    5. {
    6.     public float movementSpeed = 1f;
    7.     IsometricCharacterRenderer isoRenderer;
    8.     Rigidbody2D rbody;
    9.     private void Awake()
    10.     {
    11.         rbody = GetComponent<Rigidbody2D>();
    12.         isoRenderer = GetComponentInChildren<IsometricCharacterRenderer>();
    13.     }
    14.     // Update is called once per frame
    15.     void FixedUpdate()
    16.     {
    17.         //Vector2 currentPos = rbody.position;
    18.         Vector2 inputVector = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    19.         inputVector = Vector2.ClampMagnitude(inputVector, 1);
    20.        // Vector2 movement = inputVector * movementSpeed;
    21.         inputVector.x *= horizontalMovementSpeed ;
    22.         inputVector.y *= vecticalMovementSpeed ;
    23.         //Vector2 newPos = rbody.position + inputVector * Time.fixedDeltaTime;
    24.         isoRenderer.SetDirection(inputVector); // .normalized maybe?
    25.         rbody.MovePosition(rbody.position + inputVector * Time.fixedDeltaTime);
    26.     }
    27. }
    Something like this should do it, no need for all that extra variables, it's wasted GC time.
    also, i found that using transform.position instead of the rigidbody.position is a lot cheaper in terms of overhead and returns the same values. (tested on 5.6, may have changed by now)
     
    xaedriex likes this.