Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by 0mr_ashutosh0 · Dec 06, 2014 at 08:23 AM · rotationrotateaxisrotatearound

Snap the rotation of object

I am trying to snap the rotation of object to left and right, as i keep on clicking i dont know my there is some additional angle is being added everytime i click.

alt text

This is weird because after pressing W several time there is some additional angle is being added 104.83 is suppose to be 90, not 104.

what i am trying to do is to move object continuously in the right on x axis. and then with W, D, S keys i want them to rotate in there direction.( i am not using GetAxis because input.Getkey will be replaced by Touch after completion of game for mobile).

This is how i wont them to move

alt text

This is my script

 using UnityEngine;
 using System.Collections;
 
 public class PlayerScript : MonoBehaviour {
 
     public float speed;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         transform.Translate (Vector3.right * speed * Time.deltaTime);
 
         if (Input.GetKeyDown (KeyCode.W)) 
         {
             transform.Rotate (transform.rotation.x,transform.rotation.y,transform.rotation.z + 90f);
         }
     }
 }


clicks of w.png (21.3 kB)
problem.png (18.9 kB)
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
Best Answer

Answer by b1gry4n · Dec 06, 2014 at 08:50 AM

I am assuming the way you have your gameobject set up...

  • I notice you are rotating on the Z axis, I will do the same

  • 0 is up

  • 90 is right

  • 180 is back

  • 270 is left

    if (Input.GetKeyDown (KeyCode.W)) { transform.localEulerAngles = new Vector3(0,0,0); }

    if (Input.GetKeyDown (KeyCode.A)) { transform.localEulerAngles = new Vector3(0,0,270); }

    if (Input.GetKeyDown (KeyCode.S)) { transform.localEulerAngles = new Vector3(0,0,180); }

    if (Input.GetKeyDown (KeyCode.D)) { transform.localEulerAngles = new Vector3(0,0,90); }

By plugging in 0 for the other rotation values and not just adding 90 to our desired rotation, it will be impossible to get anything other than the desired values.

Since the rotation value is not being increasing/decreasing with increments and since the rotation value will not exceed 360, we can use eulerAngles instead of rotation. This allows us to plug in vector3 values(which I find easier to read/understand)

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 0mr_ashutosh0 · Dec 06, 2014 at 11:01 AM 0
Share

Hey @b1gry4n I am trying to rotate the the object but smoothly, Trying various combination of Quaternion but its not working. Which of the Lerp accepts current.rotation future.rotation and a float ?

i could not find a lerp which accepts angle

please help.

avatar image
0

Answer by 0mr_ashutosh0 · Dec 06, 2014 at 09:06 AM

thank you @b1gry4n it worked like a charm !!

Comment
Add comment · 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

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

26 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

Related Questions

rotate object around another object 1:1 1 Answer

Need help with rotation script. 1 Answer

rotate around character 1 Answer

Make a Move Vector rotate around axis to run animation. 0 Answers

rotate around character 2 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