- Home /
Ground Check confusion with Triggers
Hi, I'm not a programmer, but I would like some help on making triggers for my project. See, I have triggers for a ground check on my objects and in the script they all have to be variables.
var BallRigid:Rigidbody;
var Ball:Collider;
var isGrounded : boolean = false;
var speed = 40.0;
function Update () {
var cameraTransform = Camera.main.transform;
var forward = cameraTransform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
var right = Vector3(forward.z, 0, -forward.x);
if (Input.GetKey ("w")) {
rigidbody.AddForce(forward * speed * (Time.deltaTime* Acceleration));
}
if (Input.GetKey("s")) {
rigidbody.AddForce(forward * -speed * (Time.deltaTime*Acceleration));
}
if (Input.GetKey ("a")) {
rigidbody.AddForce(right * -speed * (Time.deltaTime*Acceleration));
}
if (Input.GetKey ("d")) {
rigidbody.AddForce(right * speed * (Time.deltaTime* Acceleration));
}
if (isGrounded){
if (Input.GetButtonDown ("Jump")) {
rigidbody.velocity = rigidbody.velocity + Vector3 (0,Jump,0);
}
}
if (Input.GetKey ("r")){
BallRigid.transform.position.x = 0;
BallRigid.transform.position.y = 0;
BallRigid.transform.position.z = 0;
BallRigid.rigidbody.velocity = Vector3 (0,0,0);
}
}
function OnTriggerStay () {isGrounded = true;}
function OnTriggerExit () {isGrounded = false;}
That's the script I use for the movement and ground check of my Marble/Ball in the game. Everything works but I have to declare every single thing that I can jump off of. Is there an easier way to do this with tags or the same variable for multiple objects or something like that? Thanks.
Answer by Chris D · Jun 17, 2011 at 06:09 PM
function OnTriggerStay(other:Collider){
if(other.tag == "jumpable")
isGrounded = true;
}
As long as you tag your objects as "jumpable", you should be fine.
Your answer
![](https://koobas.hobune.stream/wayback/20220612110820im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is there a tutorial for an endles-runner in 2 directions? 0 Answers
how to cancel OnTriggerExit when already hitting a 2nd trigger 2 Answers
How can I detect multiple triggers on same gameobject at once? 3 Answers
Need help with setting all items of a specific tag to "inactive" when i die. 1 Answer
Unity 2017 Not finding prefab tag 1 Answer