I’m trying to make it to my gun applies a force at the point of the raycast.
Here’s what I tried, but got a null reference exception.
function FixedUpdate () {
var fwd = transform.TransformDirection (Vector3.forward);
var hit : RaycastHit;
if(Input.GetMouseButtonDown(1)){
if (Physics.Raycast (transform.position, Vector3.forward, hit)) {
var pos = hit.point;
hit.rigidbody.AddForceAtPosition(Vector3.forward, pos);
}
}
}
Gun has no collider.
I put the script on an empty gameobject too.
It only pops up with the null reference thing when I look at my crate model (with rigidbody) and right click.
function FixedUpdate () {
var fwd = transform.TransformDirection (Vector3.forward);
var hit : RaycastHit;
if(Input.GetMouseButtonDown(1)){
if (Physics.Raycast (transform.position, Vector3.forward, hit)) {
hit.rigidbody.AddForceAtPosition(Vector3.forward, hit.point);
}
}
}