Search Unity

Help with Rotation

Discussion in 'Scripting' started by marval, Dec 3, 2014.

  1. marval

    marval

    Joined:
    Jul 10, 2013
    Posts:
    46
    hi,
    I'm trying to make a ball roll using movement and rotation ( without physics )

    the same way using a rigid body sphere
    here the ball roll in the same direction of the ball launcher
    rigidbody.AddForce(ballLauncher.transform.forward * 500 )

    I managed to make the movement using:
    transform.Translate(ballLauncher.transform.forward * 10);

    but I do not know which rotation command i use:
    to roll the ball in the same direction ( where fit ballLauncher.transform.forward )
    and
    to rotate the ball only on x and z axis ( to simulate the rolling )
     
    Last edited: Dec 17, 2014
  2. marval

    marval

    Joined:
    Jul 10, 2013
    Posts:
    46
    hi,
    anyone?
     
  3. BBeck

    BBeck

    Joined:
    Jan 12, 2014
    Posts:
    57
    The Transform of a game object holds its position as well as its orientation. You want the ball to spin, so I would think it would be a matter of rotating the ball as it moves in the direction of rotation.

    So, I would think you would use the Transform.Rotate method.

    I would have to go try and make a sample program and test it out to know how to do it.

    I mean, I could definitely do it by rotating the transform. I'm just not sure how to code that in Unity off the top of my head and I don't have any example code handy.

    Hopefully, I'll be producing a video on how this works at a very low level this month. We'll see.

    Anyway, you probably need it to spin around its local axis, in the direction of travel, at the exact same rate that it is moving in on the surface of the sphere.

    I've got an XNA program where I animate car wheels. And in that I let the wheel rotation rate determine the speed of the vehicle rather than the other way around.

    So I did WheelRotationsPerSecond * FractionOfASecondSinceTheLastFrame was drawn giving the Rotations during this frame.

    Then I multiplied WheelRotationPerFrame * (2*Pi*TheWheelsRadiusInMeters) to get The car's velocity in meters per frame. 2Pi is a full circle and multiplying the radius of the wheel gives the circumference of the wheel.

    For the rotation angle this frame, I multiplied negative WheelRotationPerFrame * (2*Pi). I assume the negative was to make it rotate in the proper direction clockwise or counterclockwise. I think this gives the rotation angle in radians rather than degrees.

    Then to rotate the object, I moved the object back to the origin by translating (not to be confused with transforming) the position of the object by its negative position. Subtracting the position from the position sets it to 0 which is the origin. I recorded the position first though.

    Then I rotated it by the correct number of radians. In my case, I didn't have to worry about determining the axis because its a wheel and it always rotates on the same local axis. I rotated it by creating a rotation matrix/transform
    and multiplying that times the current object's matrix/transform.

    Then I set the object's position back to where it was originally by translating the object and adding the position back as a translation matrix which I multiplied times the object's matrix/transform.

    And the wheels rotate at the perfect speed to make them look like they are rolling across the ground even though I'm rotating them mathematically and they are not really touching anything including the ground.

    Unity may be a bit different. There may be something in Unity physics you can use. I know what I described will work, but is it the Unity "preferred" way of doing it. Have they included something to make it easier than all that?

    I probably would have kept to myself on this one since I don't have a good answer. But I saw no one had answered you and figured my answer is better than nothing. Hope it helps.