- Home /
Making My Raycast Face The Right Direction
I am currently attempting to create a sentry-type enemy for my (top-down) game. What I want it to do is have four raycasts - one on its left side, right side, forward side, and back side from a top-down view. If these raycasts are triggered, it is to fire a bullet.
I have all of this under control, save one thing: How am I supposed to make the raycast face the desired direction?
Here's my code:
var hit : RaycastHit;
public var rightBullet = gameObject;
function Update () {
if(Physics.Raycast(transform.position,transform.rotation.y,hit)){
Instantiate(rightBullet,transform.position,transform.rotation, 7);
}
}
What I can't seem to figure out is how to get the transform.rotation.y to be 90, -90, 180, and 0. To clarify a possibly murky misinterpret point, I plan on writing four scripte - one for each raycast.
I have never done anything with raycasts before (Heck, this is day four of my first game), so any help would be appreciated. Thanks!
Answer by Piflik · Jun 29, 2012 at 09:11 PM
You don't have to rotate the object to shoot a ray in a specified direction.
(-)transform.right
(-)transform.forward
Answer by aldonaletto · Jun 30, 2012 at 12:48 AM
The second parameter in Physics.Raycast is wrong - it should be a vector pointing the raycast direction. You could do the following:
var rightBullet: GameObject; // drag the bullet prefab here var bulletSpeed: float = 20; private var hit : RaycastHit;
// this function does the raycast and shoots if something is there
function RaycastAndShoot(dir: Vector3){ // if raycast in the specified dir hits something... if (Physics.Raycast(transform.position, dir, hit)){ // create the bullet rotated to the specified direction Instantiate(rightBullet, transform.position, Quaternion.LookRotation(dir)); // accelerate the bullet in the specified direction rightBullet.velocity = dir * bulletSpeed; } }
function Update () { RaycastAndShoot(transform.forward); RaycastAndShoot(transform.right); RaycastAndShoot(-transform.forward); // equivalent to "transform.backward" (doesn't exist) RaycastAndShoot(-transform.right); // equivalent to "transform.left" (doesn't exist) }
Answer by Wolfram · Jun 29, 2012 at 09:17 PM
Don't use individual components of transform.rotation, they have nothing to do with axes, they are part of an internal Quaterion representation. Read the docs.
Furthermore, transform.rotation.y would have been a float, while a direction must be a Vector3. I stronly recommend to add "#pragma strict" as the first line to every .js script you work with. This will produce compiler errors which will tell you that something is wrong.
To actually fix your problem, use transform.right and transform.forward, wich will be the Vector3 directions of your object's local X and Z axis. For example, -transform.up will be -Y.
Your answer
Follow this Question
Related Questions
rotating ray with player rotation. 2 Answers
Raycasting from object rotation? 1 Answer
How to incorporate a rotation towards mouse position in this script? I tried 0 Answers
C# Raycast from Object direction (z-axis) + another Vector3 1 Answer
How to make an object rotate to raycast hit from mouse? Unity 3D 1 Answer