Search Unity

Does anyone know why the y isn't working?

Discussion in 'Scripting' started by Real_BTN4, Oct 21, 2020.

  1. Real_BTN4

    Real_BTN4

    Joined:
    Oct 16, 2020
    Posts:
    23
    The x works and it makes the player walk horizontal but the y just moves it horizontal too.
    Can someone help me?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public float speed;
    8.  
    9.     private float x;
    10.     private float y;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.  
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         x = Input.GetAxis("Horizontal");
    22.         y = Input.GetAxisRaw("Vertical");
    23.         transform.position += (Vector3)new Vector2(x * speed * Time.deltaTime, 0);
    24.         transform.position += (Vector3)new Vector2(y * speed * Time.deltaTime, 0);
    25.     }
    26. }
    27.  
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    transform.position is a Vector3
    so you're adding the right hand part to it via +=

    adding two vectors results in
    new Vector3(left.x + right.x, left.y + right.y, left.z + right.z);

    on your right hand expression you have a 2D Vector2 that has only xy
    you evaluate only x, and make a Vector2 that's (x, 0)

    then you cast that to Vector3 and get Vector3(x, 0, 0)
    when you add that to left hand side, everything's fine

    but then in the next line you make a Vector2 that's (y, 0)
    then you cast that to Vector3 and get Vector3(y, 0, 0)
    when you add that to left hand side, remember

    new Vector3(left.x + right.x, left.y + right.y, left.z + right.z);

    your right.x is suddenly a value for y

    just change your last line into
    Code (csharp):
    1. transform.position += (Vector3)new Vector2(0, y * speed * Time.deltaTime);
     
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    I'd also advise you not to cast vectors like that because it will lead you into problems that are hard to debug
    make a utility function instead, so that you can see it, and know what it does exactly

    i.e.
    Code (csharp):
    1. static private Vector3 to_vec3xy(Vector2 vec2) => new Vector3(vec2.x, vec2.y, 0f);
    though this seems unnecessary, trust me, you'll soon enough end up having to do
    Code (csharp):
    1. static private Vector3 to_vec3xz(Vector2 vec2) => new Vector3(vec2.x, 0f, vec2.y);
    and this tends to be confusing, just don't cross-cast between them
    also because downcasting is unsafe in Unity

    i.e.
    Code (csharp):
    1. var vec = (Vector2)new Vector3(15f, 0f, 17f); // silently ends up being (15f, 0f)
    you can also bundle this together, for clarity
    Code (csharp):
    1. transform.position += to_vec3xy(new Vector2(x, y) * speed * Time.deltaTime);
    it's the same thing and much more readable than this
    Code (csharp):
    1. transform.position += (Vector3)new Vector2(x * speed * Time.deltaTime, 0);
    2. transform.position += (Vector3)new Vector2(0, y * speed * Time.deltaTime);
    right?

    it also has fewer operations, making it negligibly faster
     
  4. VishwasGagrani

    VishwasGagrani

    Joined:
    May 12, 2018
    Posts:
    84
    I think the 2nd line should be :

    Code (CSharp):
    1.  transform.position += (Vector3)new Vector2( 0, y * speed * Time.deltaTime);