Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jason0202 · Mar 05, 2012 at 08:39 AM · raycastshootingspaceship

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! :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jason0202 · Mar 06, 2012 at 01:46 AM 0
Share

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 ;)

avatar image
2

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Mar 05, 2012 at 12:04 PM 1
Share

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.

See DontGoThroughThings

avatar image fafase · Mar 05, 2012 at 12:11 PM 0
Share

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.

avatar image jason0202 · Mar 06, 2012 at 01:42 AM 0
Share

thanks @voncarp for the reply! thanks for providing me the codes. I will try it out. Cheers! ;)

avatar image jason0202 · Mar 06, 2012 at 01:42 AM 0
Share

thanks @bunny83, its a good reference for game dev noobie like me ;)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

change to raycast shooting instead of rigidbody shooting 1 Answer

3D Space Shooter Firing Projectiles 0 Answers

See Through Objects in Top Down Shooter 2 Answers

How to Fix a Guns Firing Script so it Doesn't Constantly Fire 3 Answers

need help converting gun script from c# to javascript 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges