Search Unity

Detecting rotation/ spins

Discussion in 'Physics' started by ElliotCurtis, Dec 13, 2018.

  1. ElliotCurtis

    ElliotCurtis

    Joined:
    Nov 26, 2018
    Posts:
    20
    Hi, I've been struggling to do something that is obviously not that difficult because I don't see a thread with anyone else having the same problem!
    Basically, I have a vehicle and I want to detect if it spins, and if so, how many times.
    I've coded it once, and it detected spins that weren't there.
    Now I've done it again and it doesn't detect any.
    This is my code:

    private void TrackRotations() {
    float facing = Chassis.localEulerAngles.z;
    float angle = facing - _lastRotationPoint;
    _totalRotations += angle;
    _lastRotationPoint = facing;
    var rotCnt = TotalNumberOfRotations;
    if (_highestRotation == rotCnt) {
    return;
    }

    Any help would be greatly appreciated because this is driving me nuts now!
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    First help, is to use Proper code tagging
    Using code tags properly

    But regarding question, RigidBody has angular velocity, which may be of use for you.
     
  3. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    Facing is localEulerAngles.y (yaw). The z angle is the roll, and the x angle is the pitch.
     
  4. ElliotCurtis

    ElliotCurtis

    Joined:
    Nov 26, 2018
    Posts:
    20
    Yeah, that's what I thought. A 2d object will spin on the z axis, but it's not being registered....