I have a 2D game in Java Script. it is top down, and enemies will start to chase and attack you when nearby. I’m trying to use SphereCast to find the player (Being shot from the enemy)
I don’t know what is wrong. No matter how close I put it to the player, it never prints anything like I want it too. What did I do wrong?
#pragma strict
var player : Transform;
function Start() {
}
function Update(){
var foundHit : boolean = false;
var hit : RaycastHit;
foundHit = Physics.SphereCast(transform.position, 9999,transform.forward,hit,1000);
if(foundHit) {
print("hit");
}
}
No, I just thought maybe the spherecast was hitting your enemy collider straight away and that’s why it was’t reaching the player. You could try using this: Unity - Scripting API: Debug.DrawRay to show you where the sphere is raycasting to - it might show you where it’s going wrong.
You say the game is 2D, so the player has a collider correct and if so what type of collider? For example is it a BoxCollider or a BoxCollider2D? Is it a 3D or 2D collider?
Because if it’s 2D then I am pretty sure you need to use Physics2D, not Physics. If you still want to have a sphere or in this case a circle, then you want Unity - Scripting API: Physics2D.CircleCast
I haven’t seen that DrawRay version in the docs - the one I can see has a direction and color/duration in it? e.g. DrawRay( transform.position , transform.forward , Color.white , 1.0f );
Also, you can only see the debug rays in the ‘Scene’ view as far as I remember and not in the game view.