Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by DarkFang100 · Feb 02, 2018 at 02:07 AM · cameravector3quaterniontransform.rotationcamera rotation

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SpaceManDan · Feb 02, 2018 at 04:07 AM 0
Share

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.

avatar image DarkFang100 · Feb 09, 2018 at 03:38 AM 0
Share

@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.

avatar image SpaceManDan DarkFang100 · Feb 09, 2018 at 07:18 PM 0
Share

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.

avatar image DarkFang100 SpaceManDan · Feb 16, 2018 at 01:48 AM 0
Share

@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.

avatar image
0

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

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DarkFang100 · Mar 27, 2018 at 04:51 PM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

185 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges