- Home /
How to: Casting a ray cast
Hi guys,
I'm totally new to Unity game dev. Now, I'm trying to develop a spaceship shooting game.
I'm lost when trying to do ray casting which is not trigger by mouse. For example, the spaceship will casting out a laser/rocket to hit the enemy when I type a key on keyboard.
So, could you guys showing some sample code on how to do it?
Thanks in advance! :)
Answer by fafase · Mar 05, 2012 at 10:42 AM
So you want to throw a lazer beam to your opponent, I would do it differently. Simply create a lazer prefab add a rigidbody and then you could try:
var lazerPrefab : Transform;
var lazerSpeed = 100; //or anything you want
if (Input.GetButtonDown("Jump")){
var lazer = Instantiate(lazerPrefab, transform.Find("SpawnLazer").transform.position,Quaternion.identity);
lazer.tag = "wormProjectile";
lazer.rigidbody.AddForce(transform.forward*lazerSpeed);}
Also you need to create an empty game object that you place where you want your lazer to start from (in front of your spaceship), then attach that object as your spaceship and tag it SpawnLazer. This way your spawn is now following your spaceship movement.
Press space to trow a lazer.
Try that out and say if it goes.
This way you can also change the lazer afterwards (for upgrade for instance).
As far as I know about raycast, it is just a line so it won't look nice. Also, you are more likely to use it for information (is there something on your way) or when you are dealing with high velocity object (check for overlapped testing about this).
Hope that helps already.
thanks @fafase again~ your code really helps me alot to figure out using rigidbody as solution is much more simple. I've added @voncarp codes:
var raydirection = enemy.transform.position - transform.position;
to find the enemy direction then shoot out projectile because my rocket will auto find the enemy and destroy it ;)
Answer by voncarp · Mar 05, 2012 at 10:36 AM
The simple part of what I think your asking for is shooting the ray itself. The following java script will shoot a ray from a gameobject you attach it to towards another enemy gameobject when you hold the r key:
var enemy : GameObject;
function Update () {
var raydirection = enemy.transform.position - transform.position;
if(Input.GetKey(KeyCode.R)) {
if (Physics.Raycast (transform.position, raydirection ))
Debug.DrawRay(transform.position, raydirection, Color.cyan);
}
}
You can see the actual ray through the debug.drawray. Based on what what type of armaments your spaceships have, you would want to create different methods to launch lasers and rockets.
Just a side note: Unless you play your game only in the editor, Debug.DrawLine or .DrawRay is only visible in the editor. When you build your game you won't see anything.
Collisions at very high velocities should be avoided. It's better to use a combination of both: Use a Reycast for detecting the collision and a projectile for the visuals.
Yep the other version I gave would not work on its own with a high velocity projectile. $$anonymous$$y idea was that you usually see the beam in a space game because it does not travel so fast.
thanks @voncarp for the reply! thanks for providing me the codes. I will try it out. Cheers! ;)
thanks @bunny83, its a good reference for game dev noobie like me ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613064756im_/https://answers.unity.com/themes/thub/images/avi.jpg)