- Home /
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);} }
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;
}
}
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.
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)
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!
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
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.
Oops,sorry it was a syntax error, totally my fault, I fixed it now, try again, sorry!
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.