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 Velketor · May 01, 2012 at 09:50 PM · javascriptinputplayerrotateignore

Ignore Player Input

In my script, the player hits "s" to rotate 180 degrees. However, if they press "s" again, they rotate 180 degrees again. I don't want them to be able to rotate again. What am I missing? See script below:

function Update() { if(Input.GetKeyDown("s")) { transform.Rotate(new Vector3(0,1,0),180);} }

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by ByteSheep · May 01, 2012 at 10:11 PM

I'm guessing the problem is that the player will turn 180˚ each frame. I also assume that the player is allowed to turn more than once, so say about half a sec after the first turn he can turn again.. If this is the case then the following should work:

 var timer : float = 0;
 
 function Update() { 
 
   timer += Time.deltaTime;
 
   if(Input.GetKeyDown("s") && timer > 0.5) { 
 
   transform.Rotate(new Vector3(0,1,0),180);
 
   timer = 0;
 
   } 
 
 }

If you only want the player to be allowed to turn once (and then not again), then you can just use a simple boolean. e.g.

 var ignore : boolean = false;
 
 function Update() { 
 
   if(Input.GetKeyDown("s") && ignore == false) { 
 
   transform.Rotate(new Vector3(0,1,0),180);
 
   ignore = true;
 
   } 
 
 }
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 Velketor · May 01, 2012 at 10:30 PM 0
Share

Thank you for the response. The 2nd script you listed works best for me. However, I should have been clearer. $$anonymous$$y fault.

I don't want the player to permanently lose the ability to rotate when pressing "s". I just want to make sure that if the object is already rotated into the correct position, player input from the "s" key will be ignored. If the object is not rotated in the correct position, I want the "s" key to function again.

Thanks again. Any help is greatly appreciated.

avatar image
1

Answer by Seth-Bergman · May 01, 2012 at 10:55 PM

you could simply add a check to see what the current rotation is:

var currentRotation = transform.rotation.y;

if(Input.GetKeyDown("s") && ignore == false && currentRotation < desiredMaxRotation)

(where desiredMaxRotation is a float)

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 Velketor · May 01, 2012 at 11:09 PM 0
Share

Thank you for the response. I am not sure where I should add this code. In my current script? As a new script? If I should add it to my current code, where should I place the two lines of code? I gave it a shot many times but I keep getting errors. Please let me know if you can help. Thank you!

avatar image
1

Answer by Seth-Bergman · May 01, 2012 at 11:31 PM

attach it to the object that should rotate:

 var maxRotation : float = 180;
 
 function Start()
 {
 maxRotation = transform.rotation.y + 180;
 }
 
 function Update() { 
 
 var currentRotation = transform.rotation.y;
 
   if(Input.GetKeyDown("s") && currentRotation < maxRotation) { 
 
   transform.rotation.y  = Mathf.Lerp(transform.rotation.y,maxRotation,Time.deltaTime);
 
 
   } 
 
 }

a couple of notes:

this should technically work, assuming your starting rotation y value is less than 180, since if it hits 360 it'll flip over to zero.

Not really good code overall, but should serve as an example

Comment
Add comment · Show 2 · 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 Velketor · May 01, 2012 at 11:57 PM 0
Share

Thank you. It helps me understand it better. However, I still cannot get the script to work. I get the following error:

Assets/test.js(14,71): BCE0005: $$anonymous$$ identifier: 'time'.

I will keep playing with it. Thank you so much for all your help.

avatar image Seth-Bergman · May 02, 2012 at 07:37 AM 0
Share

Oops,sorry it was a syntax error, totally my fault, I fixed it now, try again, sorry!

avatar image
0

Answer by LeonardNS · Aug 27, 2014 at 03:03 PM

I think it would be better to just use transform.Rotation and SET the rotation to 180 instead of rotate which well. . . rotate the object every time.

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

6 People are following this question.

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

Related Questions

Camera rotation around player while following. 6 Answers

How to keep position on input.get axis even when it is pressed twice? 1 Answer

am i missing the "in game" key settings "preference" strafe vs turn 3 Answers

Directional Movement - Input position calculation? 2 Answers

Need help using GUI.Button/ input storage 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