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 Jhyatt · Aug 11, 2012 at 11:50 PM · rotationcharacterspecificy axislocked

Character rotation to a specific degree

Hello everyone, I am a making a 3rd person game and in my game I would like the character to -only- be able to change it's rotation on the y axis to positive and negative 90-180-270-360. My code currently works for rotating my character an additional 90 degrees from what it's currently at. The problem occurs when I'm interacting with physic objects, it's not uncommon for my character to rotated 5-10degrees on the y axis while plowing through a bunch of cubes. I'm okay with that but I would like it if every time I changed the rotation of my character it was set specifically to 90degrees and so on rather than my current rotation + 90. Here's my code (Note the spacing appears to be off but in the code it is correct)

function Update ()

{

if(Input.GetKeyDown("e"))

{

transform.Rotate(0, 90, 0);

}

if(Input.GetKeyDown("q"))

{

transform.Rotate(0, -90, 0);

}

}

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
1
Best Answer

Answer by AnXgotta · Aug 12, 2012 at 04:20 AM

(Moved from Comments)

If you have a rigidbody applied to your object you could check freeze rotation box on the y axis in the rigidbody script. This will only allow y-axis rotation through scripting.

This code isn't very safe for the very reason you are asking this question... if the rotation get offset a bit, the rotate by +- 90 degrees will be all off.

What you could do is use the the transform.LookAt() function to look in a direction. It points the transforms z vector at the point you give it.

 function Update (){
     
     if(Input.GetKeyDown("e")){
     
         transform.LookAt(transform.position - transform.right);
 
     }
 
     if(Input.GetKeyDown("q")){
 
         transform.LookAt(transform.position + transform.right);
 
     }
 
 }

You could still run into the same problem with the offset if there is not rigidbody to freeze the y-axis rotation.

What you could do then is check your players current forward vector against world vectors that are constant and rotate using those. This would be the safest if the above rigidbody suggestion doesn't work.

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 Jhyatt · Aug 14, 2012 at 04:02 AM 0
Share

Thanks for the help! I'll put this info to good use :)

avatar image
0

Answer by watercat · Oct 04, 2018 at 11:43 AM

this is rotesion lick tetris if need if you want something like this

 #pragma strict
 
 var x : float;  
 var y : float; 
 var z : float;
 
 function Update() {
 
     if (Input.GetKeyDown(KeyCode.Q))
            { transform.Rotate(x, y, 90); }

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

9 People are following this question.

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

Related Questions

Quaternion Rotations 1 Answer

My rigidbody's y rotation is always 180 1 Answer

GameObject wont stop rotating ? 1 Answer

How to make my player to look in the direction of right joystick's input direction 0 Answers

How would I go about finding the rotation corrresponding to the force added to the y and z axis of a object? 1 Answer


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