cancel
Showing results for 
Search instead for 
Did you mean: 

Reworking Q and E for rotate Body

zylick
Protege
Hey guys,
I'm ready to ask for help. I've been trying to implement Q and E to rotate the player so that W, S, A, D move the body different directions with the base script. But something keeps snapping the rotation back to 0.

OLD CODE this forced the camera to move as well which doesn't make sense if your body is not attached.

/* ZB
// reduce by half to avoid getting ill
if (Input.GetKey(KeyCode.Q))
YRotation -= rotateInfluence * 0.5f;
if (Input.GetKey(KeyCode.E))
YRotation += rotateInfluence * 0.5f;
*/ //ZB


New Code I figured would work by rotating the ForwardDirection object that I attached as the DirXform.

private float minimumY = -360F; //ZB
private float maximumY = 360F; //ZB

float rotateInfluence = DeltaTime * RotationAmount * RotationScaleMultiplier;

if (Input.GetKey(KeyCode.Q))
{
//Get the this and rotate it to the left so -= rotateInfluence * 0.5f;
float fYRot = transform.rotation.x - rotateInfluence * 0.5f;
fYRot = Mathf.Clamp (fYRot, minimumY, maximumY);
DirXform.transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, fYRot, 0);
}

if (Input.GetKey(KeyCode.E))
{
// Get this and rotate it to the right so += rotateInfluence * 0.5f;
float fYRot = transform.rotation.x + rotateInfluence * 0.5f;
fYRot = Mathf.Clamp (fYRot, minimumY, maximumY);
DirXform.transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, fYRot, 0);
}
7 REPLIES 7

CaliberMengsk
Explorer
Have you thought about just using transform.rotate? Should make things pretty simple.


using UnityEngine;
using System.Collections;

public class QERotate : MonoBehaviour {
public float speed = 180f;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
float left = 0;
float right = 0;
if(Input.GetKey(KeyCode.Q))
{
left = 1;
}
if(Input.GetKey(KeyCode.E))
{
right = 1;
}
transform.Rotate(new Vector3(
0,
(
-(left*speed)+
(right*speed)
)*Time.deltaTime,
0));
}
}


EDIT: Ok... so I had rotate a bit wrong in my head. Apparently it is 360 degree based. So at 180 on speed, it takes 2 seconds for a full spin. This works though. I tested it.

brendenfrank
Honored Guest
Where are you putting your code? On a parent object of the OVRCameraController?

OVRCameraController needs to have whatever object you're turning added to "Follow Orientation" on the inspector if you want it to rotate your view.

zylick
Protege
This is what I figured out this morning.

if (Input.GetKey(KeyCode.Q))
{
//Get the this and rotate it to the left so -= rotateInfluence * 0.5f;
float fYRot = transform.rotation.y - rotateInfluence * 0.5f;
fYRot = Mathf.Clamp (fYRot, minimumY, maximumY);
transform.Rotate(new Vector3(transform.localEulerAngles.x, fYRot, transform.localEulerAngles.z));
}
if (Input.GetKey(KeyCode.E))
{
// Get this and rotate it to the right so += rotateInfluence * 0.5f;
float fYRot = transform.rotation.y + rotateInfluence * 0.5f;
fYRot = Mathf.Clamp (fYRot, minimumY, maximumY);
transform.Rotate(new Vector3(transform.localEulerAngles.x, fYRot, transform.localEulerAngles.z));
}


CaliberMengsk
Explorer
My script would go on the parent GameObject of the OVRCamera object. It should spin the OVRCamera when it gets rotated. I am at work and can't test it at the moment, but I'm pretty sure that's what I've done in the past.

zylick
Protege
Darn, It broke after turning 360 degree's one time. That's strange. I wonder what I did wrong.

CaliberMengsk
Explorer
transform.Rotate is additive. By that I mean it adds to the already there device. So lets say you wanted to add 3 degrees to the x axis that's at 100, you would do

transform.Rotate(new vector3(3,0,0));


Your code is getting the already set euler angle and adding to it, so if it were at 180 already, and you added the 3 more degrees, you are adding 183 degrees instead of just 3.

Since it's left and right turning only, there is no need for a clamp (unless for some reason you only wanted them to look in the one direction). Unity automatically will change the range from negative and above 360 degree values when using transform.Rotate, so there's no need to mess with it.

You code should be pretty simple knowing that. No need to get rotations that are already set. (this will have issues if you do want to clamp though.)


if (Input.GetKey(KeyCode.Q))
{
//turn left
transform.Rotate(new Vector3(0f, -rotateInfluence * 0.5f,0f));
}
if (Input.GetKey(KeyCode.E))
{
//turn right
transform.Rotate(new Vector3(0f, rotateInfluence * 0.5f,0f));
}

O-o did I make sense this time?

zylick
Protege
Yes, that works. No wonder I was like What is going on! Thank you.