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 Althaen · Nov 13, 2012 at 04:23 AM · c#physicsraycastvector3shooting

Raycast from an objects co-ordinates

I am trying to make a ray cast from an empty object at the end of a weapon barrel. The problem is the only raycasts I see being used are using ScreenPointToRay, is there a way to fire a ray from the co-ordinates of an object instead of a point on the screen?

Comment
Add comment · Show 5
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 Althaen · Nov 14, 2012 at 04:45 AM 0
Share

How do I stop it going forever because it's locking Unity up?

avatar image AlucardJay · Nov 14, 2012 at 04:53 AM 0
Share

How many raycasts are you doing?! As in this post, you can easily run 50 raycasts without any noticable difference : http://answers.unity3d.com/questions/132621/memory-requirement-of-a-raycast.html

Chances are you have left the Debug.Log in, that could be slowing it down. Comment out the Debugs

 // Debug.Log( " Ray Hit " + hit.collider.gameObject.name );

The other problem may be that you are doing the raycast in a loop. It is hard to say without seeing your code or not knowing how many objects are in your scene and using raycast.

But if it is just one raycast from the player, then even with the Debug you should have no lag.

Here is a video for raycasting : http://www.unity3dstudent.com/2010/08/intermediate-i01-raycasting/

avatar image AlucardJay · Nov 14, 2012 at 04:55 AM 0
Share

Here are some links I strongly suggest to all new users :

Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/essential-skills/

Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/beginner/

this is the YouTube link for the above as one playlist : http://www.youtube.com/watch?v=-oXYHNSmTxg&list=PL27B696FB515608D2&feature=plcp

the Unity Wiki : http://wiki.unity3d.com/index.php/Tutorials

A list of resources : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html

Helpful page with information on using Built-In Arrays and Lists :

http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

The unity wiki link above is very handy with lots of scripts and shaders too (just check out all the links down the left, and the tabs along the top : http://wiki.unity3d.com/index.php/Scripts )

http://forum.unity3d.com/threads/132628-How-to-help-the-Absolute-Beginner

avatar image Althaen · Nov 14, 2012 at 05:02 AM 0
Share

I'm using it in a coroutine and I'm using it to fire bullets from a weapon. Perhaps I should use a different way to time the bullets.

Also I want to simulate realistic bullet drop, is that possible with raycasts? Like could I fire rays at different points and angles along the flight path?

avatar image AlucardJay · Nov 14, 2012 at 05:31 AM 0
Share

Now I am confused as to what you are trying to do! Normally if you are raycasting to shoot a bullet, there is no bullet, the raycast is used to find : if it hit enemy then deduct health; if the ray hit a wall then put a bullethole image at the ray hit point.

If you are using real bullets and physics, there is no need for a raycast at all. The bullet has a rigidbody with gravity enabled, simply fire the bullet (instantiate it), set the velocity or add impulse force at the Start, then let the physics take over.

  • I'm using it in a coroutine - I have edited my answer with a bullet timer and fire script

  • want to simulate realistic bullet drop* - You should ask this as a separate question, but some of the results from this search should help : http://www.google.com/search?btnG=1&pws=0&q=unity+fire+arrow

If my answer solves the original question Raycast from an objects co-ordinates, could you please mark it as answered, thanks.

1 Reply

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

Answer by AlucardJay · Nov 13, 2012 at 06:47 AM

A ray can be cast from anywhere, ScreenPointToRay is just a method of converting screen coordinates to world coordinates.

http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

 var hit : RaycastHit;
 if ( Physics.Raycast( transform.position, transform.forward, hit, 100.0 ) )
 {
     Debug.Log( " Ray Hit " + hit.collider.gameObject.name );
 }


this will cast a ray from the object, in the direction of the object forward (+Z-axis), for a distance of 100.0 units, then print in the console the name of anything that was hit =]

Here is a video for raycasting : http://www.unity3dstudent.com/2010/08/intermediate-i01-raycasting/


Edit :

Normally if you are raycasting to shoot a bullet, there is no bullet, the raycast is used to find : if it hit enemy then deduct health; if the ray hit a wall then put a bullethole image at the ray hit point (and play an audio clip at hit point).

If you are using real bullets and physics, there is no need for a raycast at all. The bullet has a rigidbody with gravity enabled, simply fire the bullet (instantiate it), set the velocity or add impulse force at the Start, then let the physics take over.

  • I'm using it in a coroutine - Check below for a bullet timer and firing script.

  • want to simulate realistic bullet drop* - You should ask this as a separate question, but some of the results from this search should help : http://www.google.com/search?btnG=1&pws=0&q=unity+fire+arrow

If my answer solves the original question Raycast from an objects co-ordinates, could you please mark it as answered, thanks.

here is a script just for firing bullets with a delay between each bullet :

 public var bulletObject : Transform;
 public var bulletSocket : Transform;
 
 private var bulletTimer : float = 0.0;
 public var bulletTimerMax : float = 0.06;
 
 function Update() 
 {
     ShootBullet();
 }
 
 function ShootBullet() 
 {
     bulletTimer -= Time.deltaTime;
     
     if ( Input.GetMouseButtonDown(0) )
     {    
         if ( bulletTimer < 0.0 )
         {
             Instantiate( bulletObject, bulletSocket.position, bulletSocket.rotation );
             
             bulletTimer = bulletTimerMax;
         }
     }
 }
Comment
Add comment · Show 7 · 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 Althaen · Nov 14, 2012 at 06:18 AM 0
Share

Well the issue with rigidbodies is that sometimes they clip through objects and I don't want that.

avatar image AlucardJay · Nov 14, 2012 at 06:20 AM 0
Share

Huh? The point of rigidbody is to Not clip through objects, the only case where this could happen is 1/ if you use transform.position ins$$anonymous$$d of forces; 2/ if the rigidbody is set to is$$anonymous$$inematic = true; 3/ if the rigidbody collider is set to trigger

http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.html

Rigidbody components take control over an object's position - it makes the objects fall down under the influence of gravity, and can calculate how objects will respond to collisions.

avatar image Althaen · Nov 14, 2012 at 06:23 AM 0
Share

If it moves too fast it can miss the object entirely.

avatar image AlucardJay · Nov 14, 2012 at 06:29 AM 0
Share

Ok, yes that is true, but there are work-arounds to that, e.g. http://wiki.unity3d.com/index.php?title=DontGoThroughThings

If your bullet is travelling that fast, do you really need it to fall? Unless it is a sniper round travelling a great distance, but then you should include many other factors; wind and air resistance, coriolis effect, etc.

Have a look at some of the arrow links in the search I provided, some reading may clarify what you are trying to do, and how others have approached the problem.

Again, this is a different question, and also for more traffic you should really ask this as a new question. Raycast from an objects co-ordinates has been answered.

avatar image Althaen · Nov 15, 2012 at 01:19 AM 0
Share

I'm intending to have large maps and want bullets to have a realistic flight time. But yeah that's a different question. Thanks for the help.

Show more comments

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

10 People are following this question.

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

Related Questions

AI Shooting for Aircraft 1 Answer

Rotating a raycast around the x,z axis 1 Answer

Linerenderer doesn't match my ray. 1 Answer

Using Lerp in a Constant Moving Object 1 Answer

using A* pathfinding to go to player after raycast has seen it - c# 0 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