Need to rotate camera by checking for input then setting the camera's transform rotation += to the new vector3 but it won't work???
Ok. So I'm trying to get the camera to rotate when the player pressed the left and right arrow keys (y rotation). But the problem is that I can't do a thing where I do cameratransformrotation += newvectorwithadditionalrotation. Here's the script I've tried:
void Update ()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
GetComponent ().transform.rotation.eulerAngles += new Vector3 (0, -1, 0);
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
GetComponent ().transform.rotation.eulerAngles += new Vector3 (0, 1, 0);
}
Help me please. BTW I've also tried a couple other variants, such as Quaternion += new vector3 and Quaternion += Quaternion.eulerAngles but nothing has worked. I really want to finish the game that I am making as I am almost done. It is a golf game, so plzzzzzz help mehhhh. Thanks in advance.
Answer by SpaceManDan · Feb 02, 2018 at 04:00 AM
It is my opinion that you should never use euler angles for rotations unless you have no other option. You'll always end up with issues and I can promise you that.
Instead why not just use transform.Rotate Transform.Rotate
This will "Add" to a rotation the exact way you are attempting but will account for gimbal locking as it uses a quaternion for the rotation (4 vectors instead of 3). So for example, if you want to rotate y + 1 you would use
Transform rotateMe = this.transform; //grab the transform the script is attached to.
rotateMe.Rotate((0, 1, 0) * Time.deltaTime); //rotate it by y+1
If the object you need to rotate is not the object you have the script attached to no worries.
Add this variable outside of any methods. These are usually collected at the top of the script.
public Transform rotate$$anonymous$$e;
now save the script, open the inspector and navigate to the game object the script is attached to. Find the newly added variable "rotate$$anonymous$$e" within the script information and drag the instance of the object you want to rotate into the field. Just drag and drop. Now, what ever objects transform is added to this field will be changed by the script.
@Space$$anonymous$$anDan Ok I'm having a problem where I can't use Time.deltaTime and when I try to delete it and just rotate (0, 1, 0) it rotates each rotation value, not just the y. Help please.
Sorry I missed this. Here ya go,
Ignore time.deltaTime for now. I assuemd you would know what to do with this stuff but for now lets start with something a bit more basic.
Here is what you'll want ins$$anonymous$$d.
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow))
{
rotate$$anonymous$$e.rotation = new Quaternion(rotate$$anonymous$$e.rotation.x, rotate$$anonymous$$e.rotation.y + 1,
rotate$$anonymous$$e.rotation.z, rotate$$anonymous$$e.rotation.w)
}
ROTATE sets the rotation to exactly what you say. So you would need to "+=" or "-=" the rotation depending on the direction. That means you want to add to the rotation, not set it.
So for rotate you would basically do the same thing but it takes an eulerAngle so again like I said before, eulers are not great for rotations. I don't have any idea why I suggested Rotate to you. Sorry. You are going to have to do you're homework. You will run into gimbal lock pretty quick unless you learn how to use quaternions. They started using these because rotations get jacked up when rotating over 180 degrees or 360 and can end up gimbal locked. Wont rotate. Simply learning to add quaternions and convert them to roll, pitch, yaw can make the difference between pro-work and janky-work.
@Space$$anonymous$$anDan Still doesn't work. It rotates it way back or something like maybe the w factor is weird?? It still is moving more than one axis, and I don't know why.
Answer by fernforce · Feb 09, 2018 at 06:23 AM
float rx = transform.eulerAngles.x;
float ry = transform.eulerAngles.y;
float rz = transform.eulerAngles.z;
if (Input.GetKeyDown(KeyCode.LeftArrow)) {
transform.rotation.eulerAngles = new Vector3 (rx, ry-1, rz);
}
if (Input.GetKeyDown(KeyCode.RightArrow)) {
transform.rotation.eulerAngles = new Vector3 (rx, ry+1, rz);
}
@fernforce this script didn't work it gives me this error: /Users/fernandopire/Desktop/Unity Folder/$$anonymous$$ini Golf $$anonymous$$ania/Assets/CamControl.cs(23,23): Error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.rotation'. Consider storing the value in a temporary variable (CS1612) (Assembly-CSharp) Thanks for the suggestion.
Your answer
Follow this Question
Related Questions
Maintaining look at target between two cameras 1 Answer
Sprite between two points, facing camera? 0 Answers
Camera Reset Script 1 Answer
Saving and loading cube data 2 Answers
How to use Quaternion.Slerp with transform.LookAt? 3 Answers