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.
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).
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
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