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
1
Question by Dreave · Oct 31, 2011 at 10:15 PM · raycast

Ray Casting Tutorial?

Ive looked everywhere for a simple tutorial on ray casting but all I have found is the script reference page which dosent really help with what I need. But I don't know anything about ray casting. Instead of having a bullet prefab coming out of my gun I have heard a ray cast would be better, can anyone help me with this?

Comment
Add comment · Show 2
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 Rod-Green · Oct 31, 2011 at 10:53 PM 0
Share

Are you intending to use the raycast for the impact/damage calculation i.e. to tell if it hit an object? Or are you more interested in working out how to represent visually a bullet co$$anonymous$$g out of a gun?

avatar image CC Inc · Mar 17, 2012 at 02:03 AM 0
Share

How would you do this if you wanted to visually represent a bullet?

3 Replies

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

Answer by aldonaletto · Oct 31, 2011 at 11:12 PM

Slow projectiles like rockets or cannon balls may be instantiated. For fast projectiles like bullets, it's better to use a Raycast instead (in the First Person Tutorial, the rockets are instantiated, while the machine gun uses raycast). The script below shows how to use raycast to shoot: when the Shoot function is called, a Raycast is done in the weapon forward direction; if nothing hit, nothing happens; if it's an enemy, you can instantiate blood at the hit point and send a message to make the enemy reduce its health; if another thing is hit, you can emit sparks at the hit point.

var shotSound: AudioClip; // drag a shot sound here, if any var bloodPrefab: GameObject; // drag the blood prefab here, if any var sparksPrefab: GameObject; // drag the sparks prefab here, if any

function Shoot(){ if (shotSound) audio.PlayOneShot(shotSound); // play the shot sound var hit: RaycastHit; if (Physics.Raycast(transform.position, transform.forward, hit)){ // prepare rotation to instantiate blood or sparks var rot = Quaternion.FromToRotation(Vector3.up, hit.normal); if (hit.transform.tag == "Enemy"){ // if enemy hit... if (bloodPrefab) Instantiate(bloodPrefab, hit.point, rot); // make it bleed... hit.transform.SendMessage("ApplyDamage", 5, SendMessageOptions.DontRequireReceiver); // and consume its health } else { // otherwise emit sparks at the hit point if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot); } } } This script shoots in the forward direction of the object to which it's attached. If the weapon model has a different orientation, child to it an empty object and align it to the shooting direction, then attach the script to this object.

NOTE: You must add a ApplyDamage function to the enemy, like this:

var health = 100;

function ApplyDamage(damage: int){ health -= damage; if (health

Comment
Add comment · Show 2 · 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 Dreave · Nov 01, 2011 at 05:32 PM 0
Share

I have done everything you have said but nothing happens; my enemy does not take damage and there are no blood spark effects. I have added the two scripts onto an empty game object and enemy but i cannot get it to work, please can you help?

avatar image aldonaletto · Nov 01, 2011 at 07:06 PM 1
Share

How are you calling the Shoot function? Usually you should use something like this in the same script where Shoot() is:

 function Update(){
     if (Input.GetButtonDown("Fire1")){
         Shoot();
     }
 }

The script above should be attached to the weapon, or to an empty object childed to the weapon, and the shooting direction is the "blue arrow" (in the Editor) of this object. The enemy also should be tagged "Enemy".

avatar image
1

Answer by falconer · Jun 11, 2013 at 01:14 PM

This is the best raycasting post I've come across link text

Comment
Add comment · 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
0

Answer by RetepTrun · Nov 01, 2011 at 08:40 PM

This ai uses raycast http://vimeo.com/9304844

Comment
Add comment · 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

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

RayCast Loop? 2 Answers

RayCast Delay? 1 Answer

Raycast Help 1 Answer

Finding Distance between Angles and Points 2 Answers


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