Script To Toggle Seated Position Works Intermittently

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:

  1. Check if the player is within a sphere collider trigger attached to a chair
  2. If the player is, allow them to hit control to sit
  3. if they hit control, a ‘seated’ variable is toggled to true
  4. 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

Your problem is that OnTriggerStay is on the physics timer, and wont necessarily run every frame.

You could restructure your code so that your sitdown() method is called in the Update() method (which is called every frame).

Off the top of my head:

	private bool insideSphere = false;

	void Update()
	{
		if (insideSphere)
		{
			if (Input.GetKeyDown (KeyCode.LeftControl)) // This check needs to be in Update()
				sitdown ();
		}
	}

	void OnTriggerStay(Collider collider) // In case you're spawning inside sphere
	{
		if (!insideSphere && collider.gameObject.name == "player")
			insideSphere = true;
	}

	void OnTriggerEnter(Collider collider)
	{
		if (collider.gameObject.name == "player")
			insideSphere = true;
	}

	void OnTriggerExit(Collider collider)
	{
		if (collider.gameObject.name == "player")
			insideSphere = false;
	}

Noticed you’re using UnityScript, but you get the point :slight_smile:

EDIT: And ofc, removing the input check the sitdown() method since it’s being checked for before calling it. ERR… Or just calling the unchanged sitdown method in Update (with input check inside sitdown method). Whatevs.