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,
[9334-screen+shot+2013-03-25+at+7.28.15+pm.png|9334]