- Home /
Raycast not applying force to point
The force is not being applied when I click the mouse and I can not figure out why.
// Sets the guns object
var gunActive : boolean;
var gunInHand : boolean;
var gunHoldPostion : Transform;
var inGunPickupZone : boolean;
var gunEnd : Transform;
var gunModel : GameObject;
var gunEndPost : Vector3;
var gunEndDirection : Vector3;
var zoomedIn : boolean;
var gunLine : boolean;
var sparksObject : GameObject;
var sparks : Transform;
var pokeForce : int;
var rayColor : Color;
var zoomCam : Camera;
var camSmooth : float;
var zoomCamPost : Transform;
var zoomCamPostDefault : Transform;
function Start ()
{
sparksObject.SetActive(false);
zoomedIn = false;
}
function OnTriggerEnter (col : Collider)
{
if(col.gameObject.tag == "Player")
{
inGunPickupZone = true;
Debug.Log("In Zone!");
}
}
function FixedUpdate ()
{
gunEndPost = gunEnd.position;
if(Input.GetButtonUp("Action Key") && inGunPickupZone.Equals(true))
{
gunActive = true;
}
else if(Input.GetButtonUp("Action Key"))
{
Debug.Log("Not in Pick-Up Zone");
}
}
function Update ()
{
if(gunActive.Equals(true))
{
gunModel.transform.position = gunHoldPostion.position;
gunInHand = true;
gunActive = false;
}
if(gunInHand.Equals(true))
{
gunModel.transform.position = gunHoldPostion.position;
}
// Fires red line when Priamry is active
if(gunLine.Equals(true))
{
var hit2 : RaycastHit;
var ray2 = new Ray (gunEndPost, gunEndDirection);
var forward2 : Vector3 = gunEnd.TransformDirection(Vector3.forward) * 100;
Debug.DrawRay(gunEndPost,forward2,Color.red,.01f,true);
if(Physics.Raycast(ray2, hit2))
{
Debug.Log("Red Line");
}
}
// For Primary
if(Input.GetMouseButtonDown(0))
{
var hit : RaycastHit;
var ray = new Ray (gunEndPost, gunEndDirection);
var forward : Vector3 = gunEnd.TransformDirection(Vector3.forward) * 100;
Debug.Log("Raycast was fired");
Debug.DrawRay(gunEndPost,forward,Color.green,.25f,true);
if(Physics.Raycast(ray, hit))
{
Debug.Log("Right Before force it applied");
if(hit.rigidbody != null)
{
sparksObject.SetActive(true);
hit.rigidbody.AddForceAtPosition(ray.direction * pokeForce, hit.point);
// creates a partical effect
var particleClone = Instantiate(sparks, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
Debug.Log("Force was Applied");
}
}
}
Have you checked your console window? Do you see any warnings or errors? Are your debug messages all showing up appropriately? Is AddForceAtPosition
even being called? Help us to help you.
It calls the green ray and that is it, it never reaches the "if(Physics.Raycast)"
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
var hit : RaycastHit;
var ray = new Ray (gunEndPost, gunEndDirection);
var forward : Vector3 = gunEnd.TransformDirection(Vector3.forward) * 100;
Debug.Log("Raycast was fired");
Debug.DrawRay(gunEndPost,forward,Color.green,.25f,true);
(Green Ray)
That indicates your raycast is returning false (meaning it hasn't hit anything).
I do notice one thing that might make this tricky to troubleshoot...
On lines 86-92, the data you send to Debug.DrawRay
differs from the data you send to Physics.Raycast
. In particular, the debug ray travels in direction forward
, while the physics ray travels in direction gunEndDirection
.
If those two directions don't agree, you might get unintuitive results.
Answer by MakeMeASandwich · Sep 15, 2014 at 09:54 PM
Finally fixed, forgot to make the gunholdposition(direction for gun) the direction.
(Fixed code)
if(Input.GetMouseButtonDown(0) && gunInHand.Equals(true))
{
var hit : RaycastHit;
var ray = new Ray (gunEndPost, gunHoldPostion.forward);
var forward : Vector3 = gunEnd.TransformDirection(Vector3.forward) * 100;
Debug.Log("Raycast was fired");
Debug.DrawRay(gunEndPost,forward,Color.green,.25f,true);
if(Physics.Raycast(ray, hit))
{
Debug.Log("Right Before force it applied");
if(hit.rigidbody != null)
{
sparksObject.SetActive(true);
hit.rigidbody.AddForceAtPosition(ray.direction * pokeForce, hit.point);
// creates a partical effect
var particleClone = Instantiate(sparks, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
Debug.Log("Force was Applied");
}
}
}
Thanks you rutter for helping! Much appreciated!
Glad to see you got it working. :) I've posted your response as an answer and marked it as "accepted".
Feel free to repost if you run into more problems.