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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Calculate the vertical speed of a transform in any direction

Discussion in 'Scripting' started by IMAN_SH, Aug 1, 2021.

  1. IMAN_SH

    IMAN_SH

    Joined:
    Mar 8, 2016
    Posts:
    16
    Hi.
    I have a simple game object, and I want to calculate the speed of Z-axis of the game object when it moves around the scene.

    I mean, calculation of the move forwarding velocity of the game object, in any direction.
    For more explanation, I uploaded an image:



    To doing this, I tried the below code, but since the game object rotates, (for example 90 degrees), the velocity of Z-direction comes to 0.

    Code (CSharp):
    1.  
    2. float V = 0;
    3. void Update()
    4.     {
    5.         V = V - transform.position.z;
    6.         print(V);
    7.         V = transform.position.z;
    8. }
    9.  
    How can I solve this problem?
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    IMAN_SH likes this.
  3. IMAN_SH

    IMAN_SH

    Joined:
    Mar 8, 2016
    Posts:
    16
    Kurt-Dekker likes this.