x


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

more ▼

asked May 01 '12 at 09:50 PM

shawnkilian gravatar image

shawnkilian
376 21 28 41

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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;

  } 

}
more ▼

answered May 01 '12 at 10:11 PM

merry_christmas gravatar image

merry_christmas
3.3k 5 11 17

Thank you for the response. The 2nd script you listed works best for me. However, I should have been clearer. My 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.

May 01 '12 at 10:30 PM shawnkilian
(comments are locked)
10|3000 characters needed characters left

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)

more ▼

answered May 01 '12 at 10:55 PM

Seth Bergman gravatar image

Seth Bergman
7k 10 16 28

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!

May 01 '12 at 11:09 PM shawnkilian
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered May 01 '12 at 11:31 PM

Seth Bergman gravatar image

Seth Bergman
7k 10 16 28

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: Unknown identifier: 'time'.

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

May 01 '12 at 11:57 PM shawnkilian

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

May 02 '12 at 07:37 AM Seth Bergman
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3460
x954
x804
x724
x69

asked: May 01 '12 at 09:50 PM

Seen: 604 times

Last Updated: May 02 '12 at 07:37 AM