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

Why isn't my enemy chasing the player?

Discussion in 'Scripting' started by Silverriolu295, Apr 23, 2015.

  1. Silverriolu295

    Silverriolu295

    Joined:
    Nov 12, 2014
    Posts:
    48
    So in my game, the enemy will chase the player if he enters it's raycast. The enemy detects that he is in the raycast, but won't move! I can't make the player var into a transform because it is supposed to represent the player (duh) and it won't assign it in a prefab because it's a game object!
    It is 2D (so I can't use LookAt) and I'm using Javascript.
    Code (JavaScript):
    1.   #pragma strict
    2.   var health : float = 40;
    3.   var player : GameObject;//the player game object
    4.   var speed = 0.1; //houw fast it will go
    5. var myLayerMask : LayerMask;
    6. var foundHit : boolean = false; //detects if the player enters the raycast.
    7. function Start() {
    8. }
    9.  
    10. function Update(){
    11. if(health <= 0) {  //die
    12. Destroy(gameObject);
    13. }
    14. print("enemy "+ health);
    15. //if (move == true) {
    16. //transform.position.x += this.speed;
    17. //}
    18. foundHit = false;// called every frame to reset found hit unless the player is still in the raycast
    19. var hit : RaycastHit2D;
    20. foundHit = false;
    21. foundHit = Physics2D.CircleCast(transform.position, 5,Vector2.zero,5,myLayerMask); //raycast
    22. if(foundHit == true) {
    23. //if the player is in the raycast, it should start chasing the player
    24.   transform.position = Vector2.Lerp(transform.position, player.transform.position, Time.deltaTime);
    25. //transform.position = Vector2.Lerp(transform.position, player.transform.position, speed);
    26.  
    27. print("hit");
    28.  
    29. }
    30. }
    31.  
     
    Last edited: Apr 24, 2015
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Take the time to properly indent your code. No one wants to read code that's all messy like that. It also makes spotting errors in your syntax or formatting more apparent to you as you're working.
     
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  4. Silverriolu295

    Silverriolu295

    Joined:
    Nov 12, 2014
    Posts:
    48
    my lerp worked just fine before. I just had to change the player transform to player.transform.position now it doesn't work. This article isn't helping me either. I just need it to simply move to the player over time.
     
  5. Silverriolu295

    Silverriolu295

    Joined:
    Nov 12, 2014
    Posts:
    48
    Edited the code to be a bit more clear.
     
  6. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Dont use lerp with time.deltatime. This is a common mistake. Lerp takes a value between 0 - 1 reprsenting the percetange between the values. Thus Lerp(a, b, 0) will return a while Lerp(a,b, 1) will return b. You need to increment
     
  7. Silverriolu295

    Silverriolu295

    Joined:
    Nov 12, 2014
    Posts:
    48
    Replaced time.delta time with a variable speed and it still isn't working :/
     
  8. Silverriolu295

    Silverriolu295

    Joined:
    Nov 12, 2014
    Posts:
    48
    Ok, so apparently transforms decide to work now. But now I'm getting this error message
    (28,34): BCE0017: The best overload for the method 'UnityEngine.Vector3.Lerp(UnityEngine.Vector3, UnityEngine.Vector3, float)' is not compatible with the argument list '(UnityEngine.Vector3, UnityEngine.Transform, float)'.


    Here is my new code
    Code (JavaScript):
    1.  #pragma strict
    2.   var health : float = 40;
    3.   var player : GameObject;//the player game object
    4.   var playerPos : Transform; //the transform of the player
    5.   var speed = 0.1; //houw fast it will go
    6. var myLayerMask : LayerMask;
    7. var foundHit : boolean = false; //detects if the player enters the raycast.
    8. function Start() {
    9. }
    10.  
    11. function Update(){
    12. if(health <= 0) {  //die
    13. Destroy(gameObject);
    14. }
    15. print("enemy "+ health);
    16. //if (move == true) {
    17. //transform.position.x += this.speed;
    18. //}
    19. foundHit = false;// called every frame to reset found hit unless the player is still in the raycast
    20. var hit : RaycastHit2D;
    21. foundHit = false;
    22. foundHit = Physics2D.CircleCast(transform.position, 5,Vector2.zero,5,myLayerMask); //raycast
    23. if(foundHit == true) {
    24. //if the player is in the raycast, it should start chasing the player
    25. transform.position = Vector2.Lerp(transform.position, playerPos, speed);
    26.  
    27. print("hit");
    28.  
    29. }
    30. }
     
  9. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    playerPos is a transform not a Vector3. Do playerPos.position
     
  10. Silverriolu295

    Silverriolu295

    Joined:
    Nov 12, 2014
    Posts:
    48
    THANK YOU IT WORKED!
     
  11. Silverriolu295

    Silverriolu295

    Joined:
    Nov 12, 2014
    Posts:
    48
    The only problem is the position isn't updating when the character moves