- Home /
How to stop at a point of axis?
Hi all, was wondering if some one could help me. basically i have a object and its Y axis is 8.049514 at the moment. now this object goes up and down. the down bit works perfectly. but when it starts to go back up it keeps on going. basically what i need is to find out if the gameObject has passed the Y axis position that it is set to which is 8.049514. now if this is true i just want to activate my boolean called hitfloor i want to set this to false. i would be really happy if someone could help me :) thanks in advance MCHALO
SCRIPT:
var DownSpeed : float = 20; var BackUpSpeed : float = -0.1; var StopDownSpeed : float = 0;
var hitfloor : boolean = false; function Start () {
DropStone (); }
function Update (){
var hit: RaycastHit;
Debug.DrawRay (transform.position,-Vector3.up * 5, Color.red);
if(Physics.Raycast(transform.position , -Vector3.up , hit, 5)){
if(hit.collider.gameObject.tag == "Ground"){
hitfloor = true;
//Debug.LogError("you have hit the floor");
}
}
DropStone();
}
function DropStone (){
if(hitfloor == false){ rigidbody.AddForce(-Vector3.up * DownSpeed ); } if(hitfloor == true) {
rigidbody.AddForce(Vector3.up * -BackUpSpeed );
}
if(gameObject.transform.up = 8.049514){ }
}
Answer by DaveA · May 07, 2011 at 03:16 AM
if(gameObject.transform.up = 8.049514){
First, should be == not =
Second, it should be transform.y
Third, the odds that, if it's moving, that it will be exactly this value are very small. Better to test around a range
if (gameObject.transform.y >= 8.04 && gameObject.transform.y <= 8.05)
but this will depend on the speed. Perhaps you want to detect when it passes this value in one direction or another:
if (gameObject.transform.y >= 8.04)