Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Physics.SphereCast Not Working

Discussion in 'Scripting' started by Silverriolu295, Mar 31, 2015.

  1. Silverriolu295

    Silverriolu295

    Joined:
    Nov 12, 2014
    Posts:
    48
    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?

    Code (JavaScript):
    1.  #pragma strict
    2. var player : Transform;
    3.  
    4. function Start() {
    5. }
    6. function Update(){
    7. var foundHit : boolean = false;
    8. var hit : RaycastHit;
    9.  
    10. foundHit = Physics.SphereCast(transform.position, 9999,transform.forward,hit,1000);
    11. if(foundHit) {
    12. print("hit");
    13.  
    14. }
    15. }
    16.  
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,083
  3. Silverriolu295

    Silverriolu295

    Joined:
    Nov 12, 2014
    Posts:
    48
  4. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,083
    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: http://docs.unity3d.com/ScriptReference/Debug.DrawRay.html to show you where the sphere is raycasting to - it might show you where it's going wrong.
     
  5. Deleted User

    Deleted User

    Guest

    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 http://docs.unity3d.com/ScriptReference/Physics2D.CircleCast.html
     
  6. Silverriolu295

    Silverriolu295

    Joined:
    Nov 12, 2014
    Posts:
    48
    Thank you so much! This is exactly what I needed.
     
  7. Silverriolu295

    Silverriolu295

    Joined:
    Nov 12, 2014
    Posts:
    48
    Trying to use draw ray, what am I doing wrong?
    Code (JavaScript):
    1.  function Update(){
    2. var foundHit : boolean = false;
    3. var hit : RaycastHit2D;
    4.  
    5. foundHit = Physics2D.CircleCast(transform.position, 5,Vector2.zero,5);
    6. Debug.DrawRay (transform.position, foundHit, Color.green);
    7. if(foundHit) {
    8. print("hit");
    9.  
    10. }
    11. }
    12.  
     
  8. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,083
    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.