- Home /
Why is this Coroutine not working ?
Can somebody tell me why my Shoot() coroutine is working? The cannon tracks the player perfectly but doesn't fire. Its as if it doesn't detect the player. Have I done something wrong with raycast? I'm using the built in FPS Controller, is it to do with that maybe?
(The code within the function works fine (I adapted it from another script I'm using)
var LookAtTarget : Transform;
var rotate;
var rotationDampening = 2.0;
var range = 50.0;
var distanceToPlayer;
var hit : RaycastHit;
var forward;
public var ionBallPrefab : Rigidbody;
public var ionCannonEnd : Transform;
var ionCannon : AudioClip;
function Awake()
{
LookAtTarget = GameObject.FindWithTag("Player").GetComponent(Transform);
}
function Update()
{
forward = transform.TransformDirection(Vector3.forward);
distanceToPlayer = Vector3.Distance(LookAtTarget.position, transform.position);
{
if(distanceToPlayer <= range) // Check to see if player/Target is within range
{
if(LookAtTarget)
{
rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
}
if(Physics.Raycast(transform.position, forward, hit, range))
if(hit.collider.gameObject.tag == "Player") //Shoot at Player
Shoot(); //Perform Shoot function
}
else
rotate = Quaternion.identity;
transform.rotation = Quaternion.Slerp(transform.rotation,rotate,Time.deltaTime *
rotationDampening);
}
}
function Shoot()
{
var ionBallInstance : Rigidbody;
ionBallInstance = Instantiate(ionBallPrefab, ionCannonEnd.position, ionCannonEnd.rotation);
ionBallInstance.AddForce(ionCannonEnd.forward * 900);
audio.PlayOneShot(ionCannon);
}
Suggestions please guys ?
you are storing the forward before applying rotation (if LookAtTarget). Try calculating the forward after this, or simply use transform.forward ins$$anonymous$$d of forward in the raycast.
There could be other issues such as the position of the cannons pivot, causing the ray to be cast from the base, or somewhere else where it is not reaching the player, but detecting something else first.
The first thing to always do when debugging a raycast is to see what it hit.
if ( Physics.raycast( .......... ) )
{
Debug.Log( hit.collider.gameObject.name ); // what did the ray hit??
// Do Stuff
}
I'm getting no Debug log at all ? (my bad, placed code incorrectly)
It says I'm hitting cylinder (basically the turret of the cannon) Turning off the cylinders collider and it tracks/locks correctly (get DebugLog for "Decoy")
In that case, move the origin of the raycast so it doesn't intersect with the cannon, something like :
if(Physics.Raycast(transform.position + (transform.forward * 1.5), transform.forward, hit, range)) // change 1.5 to suit
Ideally, the cannon and player colliders would be on different layers, and the raycast would be done with a layermask parameter (to ignore hits on specified layers)
http://unity3d.com/learn/tutorials/modules/intermediate/layers
http://docs.unity3d.com/$$anonymous$$anual/Layers.html
http://docs.unity3d.com/ScriptReference/Layer$$anonymous$$ask.html
http://docs.unity3d.com/$$anonymous$$anual/class-Tag$$anonymous$$anager.html
Worked fine when I just asked it to give me a Debug.Log (well at least showed me what I was hitting)
As soon as I asked it to do something I get this error-
NullReferenceException: Object reference not set to an instance of an object Turret $$anonymous$$ovement.Update () (at Assets/Scripts/Turret $$anonymous$$ovement.js:32)
$$anonymous$$ake sure you have assigned ionBallPrefab, ionCannonEnd, ionCannon, in the Inspector.
http://answers.unity3d.com/questions/47830/what-is-a-null-reference-exception-in-unity.html
http://answers.unity3d.com/questions/528288/null-reference-exception-what-is-it-and-why-do-i-g.html
Answer by HarshadK · Jan 01, 2015 at 12:38 PM
Physics.Raycast does not detect a raycast hit against the Character Controller so you need to have an other type of collider like a capsule collider on your player. This may create some unexpected behavior sometimes with the movement and collisions.
You can also try: RigidbodyFPSWalker instead of Character Controller.
Think I'm being dumb here but could you/somebody please explain that assu$$anonymous$$g I'm only 5 years old...lol
I've tried adding capsule, mesh and box colliders to the FPS controller (and the graphics component) and still nothing. Yes I got some weird movement so I tried just placing the player within range. The turret still tracked ok but didn't fire?
Do I have to remove anything from the FPS controller or simply add the new collider? (if so... where)
???
O$$anonymous$$ I tried a different approach. I made a completely new object (sphere) so it obviously has all the colliders. Reset the script to look for and track this "Decoy" I tagged it accordingly. The cannon tracks the Decoy but does not fire once locked on.
???
Raycast does not work with the Unity FPS character controller ??
Your answer
Follow this Question
Related Questions
Raycast Not Drawing In Target Direction? 0 Answers
Raycasts don't go the right direction unless i'm really far away (javascript) 1 Answer
Crash in : UnityEngine.RaycastHit:INTERNAL_CALL_CalculateRaycastTexCoord 1 Answer
Make Raycast ignore anything that "Isn't" my player(Solved) 1 Answer
Opening Door With Raycast 2 Answers