Search Unity

Question Detecting a change in y.position?

Discussion in 'Scripting' started by davebes123, May 20, 2023.

  1. davebes123

    davebes123

    Joined:
    Aug 25, 2021
    Posts:
    34
    I am trying to check if my player object is moving on an angle. I know there is Vector3.Angle but I was messing around with my own solution and came up with something.

    My code below works so far, and when I am moving up a slope, my speed is reduced. When I go from moving on an angle to a flat surface, my speed goes back to normal, however in debugging + inspector I can see that for some reason, my speed still changes occasionally despite my player.position.y not going up. Also when I first start my game, the speed changes immediately even though my y.position is not moving.

    Is there something missing from my code that is detecting a y position change when there is none?

    Code (CSharp):
    1.  
    2. Vector3 lastPosition;
    3. public Transform playerTransform;
    4.  
    5. void Start()
    6. {
    7.       playerTransform = transform;
    8.       lastPosition.y = transform.localPosition.y;
    9. }
    10.  
    11. void Update()
    12. {
    13.        if (transform.position.y > lastPosition.y)
    14.        {
    15.              Debug.Log("Speed change");
    16.              speed = 4.5f;
    17.        }
    18.        else
    19.        {
    20.             speed = 6.5f;
    21.        }
    22.  
    23.        lastPosition.y = myTransform.position.y;
    24. }
    I just don't know why when I first start the game, my debugging starts showing that there is a y.position change when there is none, but then when I step onto another flat surface, debugging stops.
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    Because you use
    localPosition
    in line 8 and
    position
    in the rest of them. When Update runs there is a difference until it gets updated at the end of the function.
     
  3. davebes123

    davebes123

    Joined:
    Aug 25, 2021
    Posts:
    34
    Yeah that's my mistake, I was meant to get rid of localPosition and replace it with just position. After doing so, the issue persists. Should I be using localPosition only instead?
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    Well you should definitely learn what is what. I can't tell you 'yes' or 'no' you have to decide for yourself, based on the requirements of what you're trying to achieve.
     
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    LocalPosition is the position of the object in its parent's local space. Depending on how you've set up your objects in the hierarchy, it might be very important or not important. It is also the actual data that rests with the object, so it's super fast to read from or write into.

    Position on the other hand, is the automatically calculated world position. A world position is what is shown after all kinds of nesting has been taken into account. This also makes it somewhat expensive (both reading and writing, because what is stored is only localPosition) so you want to avoid it, UNLESS this is exactly what you need.

    So please learn more about these systems of coordinates before you attempt to understand how to use them properly.
     
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    I don't know what kind of game you're making, but ideally you set up your agents to not be parented to anything. There are of course many exceptions to this, but that way you only ever use their localPosition and it is basically the same thing as (world) position.