cancel
Showing results forΒ
Did you mean:Β

## Reworking Q and E for rotate Body

Level 4
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 illif (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; 		//ZBprivate float maximumY 		   = 360F;			//ZBfloat 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
Level 2
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.
Level 2
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.
Level 4
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));		}		`
Level 2
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.
Level 4
Darn, It broke after turning 360 degree's one time. That's strange. I wonder what I did wrong.
Level 2
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));`

`      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));      }`