- Home /
How can I can I cast a ray from a gameobject?
So I'm trying to cast a simple ray from the spawn object, because when I tried to instantiate a bullet and fire it, it didn't work. So I decided to use a raycast instead, but it doesn't seem to work. In this scrpt I have it so that a ray is fired, and a linerenderer shows the position of where its fired, but the linerenderer doesn't move when clicked. Any help would be greatly appreciated, here is my script:
#pragma strict
var c1 : Color = Color.yellow;
var c2 : Color = Color.red;
var objecthit : GUIText;
var lineRenderer : LineRenderer;
function Start() {
lineRenderer = gameObject.AddComponent(LineRenderer);
lineRenderer.material = new Material (Shader.Find("Particles/Additive"));
lineRenderer.SetColors(c1, c2);
lineRenderer.SetWidth(0.2,0.2);
lineRenderer.SetVertexCount(2);
}
function Update () {
var fwd = transform.TransformDirection (Vector3.forward);
var origin = transform.position;
var direction = transform.forward;
var endPoint = origin + direction * 100000;
var hit : RaycastHit;
lineRenderer.SetPosition(0, origin);
if(Input.GetMouseButtonDown(0) || (Input.GetMouseButtonDown(1)) || (Input.GetMouseButtonDown(2))){
if (Physics.Raycast (transform.position, fwd, 10)) {
print ("There is something in front of the object!");
objecthit.text = "You Hit: " + hit.collider.name;
}
}
}
I thought I knew Unity well, an I'm not a beginner, but I guess I just forgot quite a bit of the basics.
Oh and I don't mean camera screen point to ray I have to have the object fired from the spawn point (the tip of the pistol) because of how I control the game.
Thanks again,
Answer by robertbu · Mar 26, 2013 at 03:33 AM
In a quick read, you have two problems. You don't pass 'hit' in to the Raycast(), so it is not initialized. So your use of 'hit' inside the 'if' statement is uninitialized. I'm surprised you did not get a runtime error. The second problem is that you never set the second point of the line renderer, so my guess is that the other end will be (0,0,0). Use a form of the Raycast() that takes a RaycastHit as a parameter, and use hit.point at the second point in the LineRenderer. As a side note, take a look in the reference for 'Transform.forward.' You don't need the TransformDirection().
what do you mean by I don't pass hit?
and how would I set the second point?
Sorry, just a weak comprehension level haha, thanks,
There are a number of different forms of the Raycast that take different parameters. You should be using the form that take the RaycastHit as a parameter.
Physics.Raycast(transform.position, fwd, hit, 100)
If you don't pass 'hit' as a parameter, then it does not get set. As for the second point, you would something like this inside the 'if' statement:
lineRenderer.SetPosition(1, hit.point);
Works almost perfectly,
one more question though, if it doesn't hit any object, (just the sky) then nothing should show up, right?
Thanks,
The way your current code is setup, the last LineRenderer settings will show up. You probably want to disable the line renderer if it does not hit anything, or you could cast a ray out a fixed distance if it misses.
alright thanks, I have an easy question for you:
http://answers.unity3d.com/questions/424881/proper-recoil-for-gun.html
youve answered a ton of $$anonymous$$e in the past, thanks
Your answer
Follow this Question
Related Questions
Raycast returns null for no apparent reason 0 Answers
Need Help with RayCast. No Vector? 1 Answer
Shoot off multiple raycasts from 1 object? 2 Answers
Raycast goes through 1 Answer
Raycast visible bullets 2 Answers