- Home /
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.
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 :)
Answer by Instinct · Feb 21, 2013 at 06:26 PM
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!
Answer by legion_44 · Feb 21, 2013 at 02:47 PM
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 ;)
Answer by Lockstep · Feb 21, 2013 at 01:54 PM
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.
I think your code should be good. But for some reason I get an error saying: "Cannot implicitly convert type 'UnityEngine.Quaternion' to 'UnityEngine.Vector3'"
I think it means that you can't convert Quaternion to Vector3.
Halp pls :)
Ah sorry. I messed up a bit. targetRotation should be a quaternion but I accidentially declared it as vector. I'll edit it.
Wow, it works perfectly (better than the solution I found!). Just small comment, you have a typo: You have a capital T in your targetRotation at the last line in you code.
Will be using this!
Just a small question, after my player cube rotates it kinda gets submerged into the platform cube (the ground).
This causes problems for me, as the [pseudo] if(touchGround){ /JU$$anonymous$$P code here / } doesn't work properly (the game doesn't see that I'm actually touching the platform).
Got any workarounds or tips for me?
(I have the if(touchGround){jump} so I can't jump while im in the air).
This happens because manipulating rigidbody.rotation overrides PhysiX, thus ignoring collisions. You would need to use rigidbody.AddTorque if you want to prevent that. Although this might come with other inconveniences like bouncing in an unwanted angle.
Your answer
![](https://koobas.hobune.stream/wayback/20220613093108im_/https://answers.unity.com/themes/thub/images/avi.jpg)