Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Enemy is moving through walls

Discussion in 'Scripting' started by SgtBossGamer12, Jan 20, 2015.

  1. SgtBossGamer12

    SgtBossGamer12

    Joined:
    Oct 2, 2014
    Posts:
    16
    I'm not very good with javascript, especially with AI. I have an AI script that makes the enemy follow the player, and when the enemy gets to a certain distance, the level restarts. The script works, except my enemy walks through walls. I've added a rigidbody, and it still doesn't work.
    Code (JavaScript):
    1. var Player : Transform;
    2. var MoveSpeed = 5;
    3. var MaxDist = 10;
    4. var MidDist = 7.5;
    5. var MinDist = 5;
    6. function Start ()
    7. {
    8. }
    9. function Update ()
    10. {
    11.     transform.LookAt(Player);
    12.    
    13.    
    14.    
    15.      if(Vector3.Distance(transform.position,Player.position) >= MinDist){
    16.    
    17.          transform.position += transform.forward*MoveSpeed*Time.deltaTime;
    18.          
    19.    
    20.  
    21.      
    22.          
    23.          
    24.          if(Vector3.Distance(transform.position,Player.position) <= MinDist)
    25.              {
    26.                 Application.LoadLevel(1);
    27.  
    28.  
    29.    }    
    30.    
    31.    }
    32.  
    33.  
    34. }
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    It's because you're moving via the Transform, not the Rigidbody.
     
  3. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    230
    Yup. Moving the transform just moves the object directly, through anything. Using the rigidbody's velocity will move it using the physics.
     
  4. SgtBossGamer12

    SgtBossGamer12

    Joined:
    Oct 2, 2014
    Posts:
    16
    Sorry it took a while for me to reply. Anyways, I'll try that. Thanks
     
  5. SgtBossGamer12

    SgtBossGamer12

    Joined:
    Oct 2, 2014
    Posts:
    16
    Now the enemy won't move at all
    Code (JavaScript):
    1. var Player : Transform;
    2. var MoveSpeed = 5;
    3. var MaxDist = 10;
    4. var MidDist = 7.5;
    5. var MinDist = 5;
    6. function Start ()
    7. {
    8. }
    9. function Update ()
    10. {
    11.     rigidbody.LookAt(Player);
    12.    
    13.    
    14.    
    15.      if(Vector3.Distance(rigidbody.position,Player.position) >= MinDist){
    16.    
    17.          rigidbody.position += rigidbody.forward*MoveSpeed*Time.deltaTime;
    18.          
    19.    
    20.  
    21.      
    22.          
    23.          
    24.          if(Vector3.Distance(rigidbody.position,Player.position) <= MinDist)
    25.              {
    26.                 Application.LoadLevel(1);
    27.  
    28.  
    29.    }    
    30.    
    31.    }
    32.  
    33.  
    34. }
     
  6. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    I'm only new, but would

    Code (JavaScript):
    1. rigidbody.AddForce (Vector3.forward * MoveSpeed * Time.deltaTime)
    work better for you?
     
    dterbeest likes this.
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Depending on what's wrong that might make it move, but it's fundamentally changing the nature of what's going on. Applying a force is not the same as directly moving something, and that's not a change I'd make except when (re)considering the design of what I'm doing.

    Stick Debug.Log(...) messages in each part of your script so you can see what is and isn't being called. Print the value of Player and print the calculated distance, and the result of your movement calculation. I can't tell what's busted, but one of those values will almost certainly be something you don't expect, and chasing that rabbit down the hole will reveal the cause of your problem.