system
1
im trying to cast a ray from my player that will detect object it touches a certain length in front of him. i swear my code is fine but i cant get it to work
var xDirection = 15;
var yDirection = 0;
var zDirection = 45;
var length = 0.1;
var hit : RaycastHit;
function Update () {
var diagonal = transform.TransformDirection (Vector3(xDirection,yDirection,zDirection));
Debug.DrawRay(transform.position, diagonal * length,Color.green);
if (Physics.Raycast(transform.position, diagonal,length)) {
Debug.Log ("There is something in front of the object!");
}
}
help would be great :)
EDIT ANOTHER PROBLEM.....
var Bullet1_direction = Vector3(15,0,45);
var Bullet2_direction = Vector3(0 ,0,1);
var Bullet3_direction = Vector3(-9 ,0,45);
var length = 4;
function Update () {
var Bullet1_diagonal = transform.TransformDirection(Bullet1_direction);
Bullet1_diagonal.Normalize();
var Bullet2_diagonal = transform.TransformDirection(Bullet2_direction);
Bullet2_diagonal.Normalize();
var Bullet3_diagonal = transform.TransformDirection(Bullet3_direction);
Bullet3_diagonal.Normalize();
Debug.DrawRay(transform.position, Bullet1_diagonal * length,Color.green);
Debug.DrawRay(transform.position, Bullet2_diagonal * length,Color.red);
Debug.DrawRay(transform.position, Bullet3_diagonal * length,Color.blue);
if (Physics.Raycast(transform.position, Bullet1_diagonal,length)) {
Debug.Log ("Bullet 1 hit green!");
}
if (Physics.Raycast(transform.position, Bullet2_diagonal,length)) {
Debug.Log ("Bullet 2 hit red!");
}
if (Physics.Raycast(transform.position, Bullet3_diagonal,length)) {
Debug.Log ("Bullet 3 hit blue!");
}
}
HELP PLEASE...
@Bunny83
i think you forgot the hit :
var xDirection = 15;
var yDirection = 0;
var zDirection = 45;
var length = 0.1;
var hit : RaycastHit;
function Update () {
var diagonal = transform.TransformDirection (Vector3(xDirection,yDirection,zDirection));
Debug.DrawRay(transform.position, diagonal * length,Color.green);
if (Physics.Raycast(transform.position, diagonal,hit,length)) {
Debug.Log ("There is something in front of the object!");
}
}
hope it works
have you attached it to the right player? if you still can not get it to work send it to me to my e-mail and i can look at it for you. the project folder this is.
My e-mail:
Hotmail- gto_oni-eyes@hotmail.com
google:
Hummad.Nazir@gmail.com
i hope to hear from you :)
try my code:
public var RayCastHitDoor :float = 5.0f;
// Update is called once per frame
function Update () {
var hit : RaycastHit ;
Debug.DrawRay(transform.position, Vector3.forward * RayCastHitDoor, Color.red);
if(Physics.Raycast(transform.position , Vector3.forward, hit, RayCastHitDoor))
{
Debug.DrawRay(transform.position,Vector3.forward * RayCastHitDoor , Color.green);
if(hit.collider.gameObject.tag == "put your obj name in here "){
// then do something. in my case i have told the hit collider to play animation when it collides with the object that is tagged door
hit.collider.gameObject.animation.Play("Door animation");
}
}
}
instead of having animation you could have Destroy the object if you want to do that i would be like this:
Destroy(hit..collider.gameObject);
and that should destroy the game object you can do what ever you like :)
I would say your problem is your direction. If you use Raycast with a length the direction is normalized! That means your ray is 0.1 units long. But if you draw your ray with `diagonal * length` the drawn ray will be 4.743 units long since your direction is 47,43... long. I don't even understand your direction. It's forward but a bit to the right? Try `diagonal.Normalize();` or `.normalized`.
var direction = Vector3(15,0,45);
var length = 0.1;
function Update () {
var diagonal = transform.TransformDirection(direction);
diagonal.Normalize();
Debug.DrawRay(transform.position, diagonal * length,Color.green);
if (Physics.Raycast(transform.position, diagonal,length)) {
Debug.Log ("There is something in front of the object!");
}
}