Hi, I can usually find what I’m looking for by browsing other answers, but this one has me a little bit stumped.
Basically, I have some JS code that is supposed to:
- Check if the player is within a sphere collider trigger attached to a chair
- If the player is, allow them to hit control to sit
- if they hit control, a ‘seated’ variable is toggled to true
- if they are seated and press control again, they are returned to their original standing position
Now, 75% of the time, the script I have works perfectly - but sometimes it just doesn’t, requiring two or three ctrl key inputs before performing the function. This happens for both ‘sit’ and ‘stand’.
var seatedposition : Vector3;
var seatedheight : float;
var standingposition : Vector3;
var standingheight : float;
var seated : boolean = false;
function OnTriggerStay(player : Collider)
{
if(player.gameObject.name=="player") {
sitdown();
}
}
function sitdown () {
if(Input.GetKeyDown(KeyCode.LeftControl)){
sit();
stand();
seated = !seated;
if (seated){
Debug.Log("seated");
}
if (!seated){
Debug.Log("not seated");
}
}
}
function sit () {
if (!seated) {
standingposition = gameObject.Find("player").transform.position;
standingheight = gameObject.Find("player/Main Camera").transform.position.y;
gameObject.Find("player").transform.position = seatedposition;
gameObject.Find("player").GetComponent(CharacterMotor).canControl = false;
gameObject.Find("player/Main Camera").transform.position.y = seatedheight;
}
}
function stand () {
if (seated) {
gameObject.Find("player").transform.position = standingposition;
gameObject.Find("player").GetComponent(CharacterMotor).canControl = true;
gameObject.Find("player/Main Camera").transform.position.y = standingheight;
}
}
Any suggestions s to where I’m going wrong appreciated.
Thanks