Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Rigidbody rotation

Discussion in 'Physics' started by Jaszczura52, Dec 4, 2014.

  1. Jaszczura52

    Jaszczura52

    Joined:
    Nov 28, 2014
    Posts:
    3
    Hello!
    I am having a bit of trouble with my character controller (I am trying to script it on my own), namely I want to freeze rotations of rigidbody being effect of collision with terrain or other static objects, but I want to still be able to rotate it using user input.
    The idea is: I wanted a third person cotroller based on FPS rigid body controller from wiki, but with ability to rotate the body with certain Input Axis (say "Rotational" axis, with buttons q and e). Ability to keep body's ability to react on applied forces, and real gravitation, possibility to change its mass and so on are too tempting to decline for me, thus I think modifications of third person controller will not satisfy me.
    Is there any way to let me rotate body according to user input but remove the chaotic rotations due to collisions with terrain or other objects?
     
  2. SpeakUpGames

    SpeakUpGames

    Joined:
    Nov 25, 2014
    Posts:
    56
    RigidBody has some checkboxes to "freeze" the position and rotation. It's kinda hidden, it was a headscratcher for me for a while too. Is this what you're looking for?
     
  3. Jaszczura52

    Jaszczura52

    Joined:
    Nov 28, 2014
    Posts:
    3
    Actually no, I believe this bool changes the body's ability to rotate this is true, but it affects all kind of rotations, and I want to be able to rotate the body with user input.

    Edit 1: Oh I am sorry, haven't read this: http://docs.unity3d.com/ScriptReference/Rigidbody-freezeRotation.html
    It seems that this indeed stop the rotations coming from "physics simulation" so here comes my other question:
    If I freeze the rotation (either in rigidbody component window or via script), how can I influence it by user input?
     
    Last edited: Dec 6, 2014
  4. SpeakUpGames

    SpeakUpGames

    Joined:
    Nov 25, 2014
    Posts:
    56
    You can either directly set the transform's rotation or use the Transform.Rotate() method.
    Code (csharp):
    1. if(Input.GetMouseButtonDown(0)){
    2.   transform.Rotate(Vector3.right * Time.deltaTime);
    3. }
    See more here: http://docs.unity3d.com/ScriptReference/Transform.Rotate.html
     
  5. Jaszczura52

    Jaszczura52

    Joined:
    Nov 28, 2014
    Posts:
    3
    It works! Thank you kind Sir for quick and helpful response!
    Have a nice day!