- Home /
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?
How do I stop it going forever because it's locking Unity up?
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/
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 :
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
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?
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.
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;
}
}
}
Well the issue with rigidbodies is that sometimes they clip through objects and I don't want that.
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.
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.
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.