- Home /
Get raycast direction from weapon
I want to use raycast to create a laser system (also for the bullets) for the weapons in my game, but right now Im just trying to draw a line to see whats going on in there. But how can I get the direction where the weapon is pointing?
using UnityEngine;
using System.Collections;
[AddComponentMenu("Rayco's scripts/Laser")]
public class laser : MonoBehaviour {
public GameObject weapon;
public Vector3 RotationOffset;
public Vector3 PositionOffset;
Vector3 rayo;
Vector3 direccion;
void Update() {
direccion = weapon.transform.forward;
rayo = weapon.transform.position;
transform.parent = weapon.transform;
Physics.Raycast(rayo, direccion, 10);
Debug.DrawLine(rayo, direccion, Color.red);
}
void start() {
transform.localEulerAngles = RotationOffset;
transform.localPosition = PositionOffset;
}
}
I don't understand your question. If you modeled your weapon so that the muzzle points towards +Z, the code you provided should already cast the ray in the correct direction. So what exactly is it you need?
Also, what are you trying to accomplish with "transform.parent = weapon.transform;"? This should be called once, not in Update().
One problem I see with your code is that reparenting does not modify the world coordinates of your object, ins$$anonymous$$d it modifies the local transformations accordingly, which will effectively override the settings you are trying to do in Start().
Another bug in your script is case sensitivity. "start()" will never be called. The function needs to be called "Start()".
Well it does not, it cast from the weapon to the ground (always to the same spot). I think the question its pretty obvious.
Did you check any of the other things I mentioned? All of the remarks were important. $$anonymous$$ost importantly, weapon.transform.forward
will point along +Z of your "weapon" node. At the top of your editor there are two buttons, one says "Pivot" or "Center", the other "Global" or "Local". Set them to "Pivot" and "Local". Now select the object you assigned to "weapon". Does the muzzle now point along the blue arrow?
It was already in Pivot and Local and it does not point to the blue arrow, but I already knew that, thats why I added offset fields so I can adjust it later. Anyways I changed all the code. Now the line moves with the character but its waaay off the weapon. I added the code to a new answer, as soon as it gets reviewed you can check it out. Thanks for your time.
Answer by robertbu · Jan 13, 2013 at 05:04 AM
As Wolfram mentioned, there are a number of issues with your code. So below is a bit of starter code. Attache the script to your gun (or a child object of your gun), drag a light onto the public lightLazer variable, and the script will place a light on objects the Raycast hits in the scene. I setup my light with a range of 0.1, intensity of 1.5 and the color red. This code cast the ray from the origin of the object it is attached to. You will need to translate the ray up to the gun sight level (or perhaps easier, attach the script to an empty game object with its origin at the level of the sight). Also if your gun has a collider, you will need to either 1) remove it, 2) use a more complex raycast, or 3) move the origin of the raycast outside the gun. Good luck with your project.
using UnityEngine;
using System.Collections;
public class Lazer : MonoBehaviour {
public Light lightLazer;
private Ray ray;
void Start ()
{
lightLazer.enabled = false;
}
void Update ()
{
RaycastHit hit;
ray.direction = transform.forward;
ray.origin = transform.position;
if (Physics.Raycast (ray, out hit))
{
Vector3 v3Pos = ray.GetPoint (hit.distance * 0.995f);
lightLazer.enabled = true;
lightLazer.gameObject.transform.position = v3Pos;
}
else
{
lightLazer.enabled = false;
}
}
}
Thanks for your code but I rather fix my code, If I use your code Im not learning anything.
I too try and not use code I do not understand. But I'm forever dropping in sample code and modifying it with it as a way to understand an approach if not the specifics for a problem. And comparing my code to a working sample has allowed to solve any number of problems.
Ok lets try again:
using UnityEngine;
using System.Collections;
[AddComponent$$anonymous$$enu("Rayco's scripts/Laser")]
public class laser : $$anonymous$$onoBehaviour {
public GameObject weapon;
public Vector3 RotationOffset;
public Vector3 PositionOffset;
Vector3 rayo;
private Ray ray;
void Start () {
rayo = weapon.transform.position;
transform.parent = weapon.transform;
transform.localEulerAngles = RotationOffset;
transform.localPosition = PositionOffset;
}
void Update() {
ray.origin = rayo;
ray.direction = transform.forward;
RaycastHit hit;
Physics.Raycast(ray, out hit, 100);
Debug.DrawLine(rayo, hit.point, Color.red);
}
}
This seems to work but the line its waaaay off the weapon (its draw under the map and pointing in a different direction). Changing the offset values doesnt seem to do anything.
Use Comment or even EDIT in this case. Questions and their elaborations are to be in guestion post.
Answers are, well for answers.
Well, if it's pointing in the wrong direction, you need to adjust your RotationOffset - but in which way is impossible to tell without knowing your scene setup, and in which world space direction your muzzle is actually pointing.
Concerning the position, note that you initialize ray.origin with weapon.transform.position, which is the world space position of your weapon's pivot. You are currently not applying PositionOffset to that position. for starters, try ray.origin = transform.position;
ins$$anonymous$$d, and also replace rayo
in the Debug-command with ray.origin
.
Ok chaged ray.origin now it works! Thanks U!