Help on rotating game object in C sharp

Hello, I’m new to unity (Started with it for two weeks ago).

I have now created a game of a box moving automatically from left to right and small jump function with rigidbody.addforce.

Now, my game should be like this: The player will move to right (without any key input). You should jump over, or duck under obstacles.
But, I can’t seem to find out how to script the “duck under” mechanic in a good way.

What I had in mind was to rotate my player cube 90 degrees, so it could slide under the obstacle. The player cube is a slim rectangle, which reminds of a human structure.
8134-illustraion.jpg

This is what I have. But doesn’t serve as a good mechanic for my game. (it’s very clunky, but it rotates it as long as you hold down the button).

public Vector3 slidePress = new Vector3(0, 0, 90);
    
void Update () {

if(Input.GetAxis("Horizontal") < 0){
			Quaternion deltaRotation = Quaternion.Euler(slidePress * Time.deltaTime);
        	rigidbody.MoveRotation(rigidbody.rotation * deltaRotation);
		}
}

Keep in mind, I’m not very good at syntax in programming, but I do understand the logic of programming. But I find a lot of what I write on the internet. So be gentle on your answers. Thanks :slight_smile:

Hi, first make Empty GO that will be pivot of your player. Set it position and attach Player Cube as children then attach this script to your pivot:

public class Rotate : MonoBechaviour {
void Rotate()
{
if(Input.GetKey("c"){
transform.rotation = Quaternion.Lerp(transform.rotation, /*Here your "duck" rotation*/);
 }
else{
transform.rotation = Quaternion.Lerp(transform.rotation, /*Here your standing rotation*/);
  }
 }
}

Change the /Here your standing rotation/ to Quaternion(x,y,z,w) where x,y,z,w is rotation when player standing.
Change the /Here your “duck” rotation/ to Quaternion(x,y,z,w) where x,y,z,w is rotation when player “ducking”.
sorry if i wried something wrong, i using JS not C# and code is not tested but should work :wink:

If you want to do something in a smooth way, you should always consider a Slerp. This could be done like this:

public float rotatingSpeed = 10f;
public Vector3 duckEulers;
private Quaternion duckRotation;
public Vector3 standEulers;
private Quaternion standRotation;
private Quaternion targetRotation;

void Start(){
    duckRotation = Quaternion.Euler(duckEulers);
    standRotation = Quaternion.Euler(standEulers); //probably enough to say = Quaternion.identity
}

void Update (){
    if(Input.GetKey("DuckKey")){
        targetRotation = duckRotation;
    } else {
        targetRotation = standRotation;
    }
    rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation, TargetRotation, Time.deltaTime * rotatingSpeed);  //This will increment the rotation towards the target rotation
}

Maybe you can also achieve the crounch effect by manipulating the upwards scale.

Update: I got somewhere around to what I want with this code (written in java):

var smooth = 1.0; 
var duck = 90.0; 
var stand = 0.0;

function Update () {
 
    if(Input.GetAxis("Vertical") < 0){
    var target = Quaternion.Euler (0,0, duck);
    transform.localRotation = Quaternion.Slerp(transform.localRotation, target, Time.deltaTime * smooth);
    }
    else{
    var target2 = Quaternion.Euler (0,0, stand);
    transform.localRotation = Quaternion.Slerp(transform.localRotation, target2, Time.deltaTime * smooth);
    }
}

If you have any improvement or suggestions then please inform me. Anyhow, thanks for all the help!