Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Count and display rotation on z axis

Discussion in '2D' started by si0ty, Aug 11, 2019.

  1. si0ty


    Jun 28, 2019
    Hey guys, im really lost on this and can't get it to work.

    So I simply want to detect the rotation of an 2D object on the z axis and add one point when a full rotation is made in both directions. Like backflips and frontflips and sum them up if more than one is done. With help of the Forum I could get this code:
    Code (CSharp):
    1.   private float totalRotation = 0;
    2.     public int nrOfRotations {
    3.         get {
    4.             return ((int) totalRotation)/360;
    5.         }
    6.     }
    7.     private Vector3 lastPoint;
    9.     void Start () {
    10.         lastPoint = transform.TransformDirection(Vector3.forward);
    11.         lastPoint.z = 0;
    12.     }
    14.     // Update is called once per frame
    15.     void Update () {
    16.         Vector3 facing = transform.TransformDirection(Vector3.forward);
    17.         facing.z = 0;
    19.         float angle = Vector3.Angle(lastPoint, facing);
    20.         if (Vector3.Cross(lastPoint, facing).z < 0)
    21.             angle *= -1;
    23.         totalRotation += angle;
    24.         lastPoint = facing;
    25.     }
    26. }
    Im really new to quaternions and don't really undestand fully whats going on here but I fiqured out how to call the NrOfRoations function from another script which displays the value on to a text field. I don't get any value from this so their is a problem with the detection of rotations. Can you guys give me any advice whats missing here? Would be so happy about any help on this.
  2. Hyblademin


    Oct 14, 2013
    Vector3.forward is (0, 0, 1), but I'm guessing you want to use Vector3.right, which is (1, 0, 0). This would track the angle of the x-axis rather than that of the z-axis. If you were to print lastPoint or facing to the console, you would probably see them returning unexpected values, which is a good way to troubleshoot this kind of problem.

    You could try using Transform.right instead. It looks like this is what you mean to be calculating anyway, but this cuts out a lot of steps and makes the logic easier to follow. Since a Transform's right vector is already given in world space, you won't need space transformations to calculate the angle.

    That said, I think it's probably easier to access the rotation rate of the object and keep a running sum from the point you want to start tracking rotation. If you're using Rigidbody physics, use Rigidbody2D.angularVelocity. If the rotation is not physics-based, there will already be a variable used to apply rotation each frame. This will eliminate the need to calculate the sign of the rotation, since you already have that information somewhere.

    Quaternions are not directly involved with the above code, and understanding them wouldn't help with troubleshooting it. A GameObject's Transform.rotation is given as a quaternion, but is not usually directly manipulated by user scripts because intuitive methods are provided for common tasks.

    A little off-topic: You have
    nrOfRotations defined as a property, not a function. I figured it's worth mentioning so that internet searches, etc. will give you the results you're looking for.
    Last edited: Aug 12, 2019
  3. si0ty


    Jun 28, 2019
    Thanks man! I really appreciate how much time you spend on your're answer and I definetly learned some things here but it couldn't really help me on my problem. I think it's to complicated for me at the moment and there is a simpler solution to this.

    For everyone with similar situations I found help on this thread:

    It's still not 100% how it should function but i'm pretty close. But thanks alot anyways! The answers and community in general is so nice an generous to beginners.