Rotate smoothly on sphere
Hi, I try to rotate my player on the y-axis smoothly when I turn left(y=-45) or right(y=45) and it feels like I've tried all the examples but my player just starts spinning. I guess one of the problems is that my player is moving constantly on a globe (sphere) and the rotation is constantly changing?
The closest I got is this but when I turn left it rotates the wrong way:
protected Vector3 leftRot = new Vector3(0, -45, 0);
protected Vector3 rightRot = new Vector3(0, 45, 0);
protected Vector3 normalRot= new Vector3(0, 0, 0);
void Update()
{
currentRotation = RendererGO.transform.localEulerAngles;
float LeftRight = 0;
if (Input.touchCount > 0)
{
// touch x position is bigger than half of the screen, moving right
if (Input.GetTouch(0).position.x > Screen.width / 2)
LeftRight = 1;
// touch x position is smaller than half of the screen, moving left
else if (Input.GetTouch(0).position.x < Screen.width / 2)
LeftRight = -1;
}
moveDir = new Vector3(LeftRight, 0, moveSpeed).normalized;
//Rotations
if (LeftRight == -1){
this.transform.localEulerAngles = Vector3.Lerp(currentRotation, leftRot, Time.deltaTime * rotatesSpeed);
}
else if (LeftRight == 1){
this.transform.localEulerAngles = Vector3.Lerp(currentRotation, rightRot, Time.deltaTime * rotatesSpeed);
}
else{
this.transform.localEulerAngles = Vector3.Lerp(currentRotation, normalRot, Time.deltaTime * rotatesSpeed);
}
}
I've tried som examples that shows Quaternion but I do not succeed... I would love to get some help with this one.
Can you please add the part where you are listening to user input and set the value for leftRot?
Thanks for the extra info. O$$anonymous$$, I haven't had time to test this on my computer but here are my suggestions:
You are listening for touchCount > 0. So if my finger touches the screen for more than a single frame, the rotation will keep getting applied over and over. Try using (if Input.Get$$anonymous$$ouseButtonDown(0) ) ins$$anonymous$$d. This will work for touch as well and it is easier to use in general as long as you do not care for mul$$anonymous$$ch functionality in your project (i.e. pinch to zoom, etc)
Because you are using localEulerAngles, the rotation is relative to the parent transform. Is this object parented to anything or is it free standing? In the latter case it may be so that as the transform rotates, so does the 45 degree offset. If your goal is to clamp the player direction between certain angles in the world you may want to use transform.eulerAngles ins$$anonymous$$d for world rotation.
Answer by taylank · Jan 15, 2016 at 12:30 AM
When you're dealing with rotation as vectors, lerp can be difficult to work with due to ambiguity in which direction to lerp in. For instance if you're at angle 0 and you want to go 30, that's fine, but if you want to go -30 instead that can be interpreted as 330 and a long lerp from 0 to 330 clockwise.
I'd recommend using Mathf.LerpAngle instead. There is a handy example you can use in the documentation.