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 walks in air

Discussion in 'Scripting' started by Soumikbhat, Jun 20, 2014.

  1. Soumikbhat

    Soumikbhat

    Joined:
    Nov 23, 2013
    Posts:
    110
    I am using a simple script that has the enemy follow the player when he's some distance apart.

    The problem is that the terrain that I am using is uneven, so the enemy does not remain on the ground when he's running, he starts to walk in the air when the lad dips down. Any way to fix this?

    The script :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AICustomed : MonoBehaviour {
    5.     public Transform enemy;
    6.     //public Transform target;
    7.     //var rotationSpeed:int=2;
    8.     //int rotationSpeed=2;
    9.  
    10.     public GameObject player;
    11.     public float distance;
    12.     public bool layer=false;
    13.     //public Animator animator;
    14.     protected Animator animator;
    15.     // Use this for initialization
    16.     void Start () {
    17.         player = GameObject.Find("Player");
    18.         //animator=GetComponent(Animator);
    19.         animator = GetComponent<Animator>();
    20.        
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update () {
    25.         if (layer==true)
    26.         {transform.LookAt(player.transform);
    27.             animator.SetBool("Run", layer);
    28.         }
    29.         distance = Vector3.Distance(player.transform.position, enemy.position);
    30.         //Debug.Log("distance is " + distance);
    31.         if(distance <10)
    32.         { layer=true;
    33.             //transform.LookAt(player.transform);
    34.             //animator.SetBool("Run", layer);
    35.         }
    36.     }
    37. }
    There's a terrain collider attached to the terrain and a character controller to the enemy.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    Apply some gravity of some sort to the enemy.

    This could be as simple as during the move action always apply some velocity in the downward position as well as towards the player.
     
  3. Soumikbhat

    Soumikbhat

    Joined:
    Nov 23, 2013
    Posts:
    110
    I tried something like :
    Code (JavaScript):
    1. var isGrounded : boolean = false;  //means it is not on the ground
    2. function Update(){
    3.     if(!isGrounded){
    4.         rigidbody.AddForce(Vector3.Up * -10);
    5.     }
    6. }
    7. function OnCollisionEnter(col : Collision){
    8.     if(col.gameObject.FindWithTag("Terrain")){
    9.         isGrounded = true;
    10.     }
    11. }
    Did not work out :(
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    You have no code determining when you left the ground. So once it hits the ground the first time, it then thinks it's grounded forever from then on out.

    Try not testing isGrounded, and just apply a downward force.

    See if that works.

    This should be fine as the collider of the ground will block it from going down (the object will in turn slide down ledges as well, if the ground is not flat).

    Now once that's working, THEN add in the ground testing, if ground testing is the behavior you desire.

    Build your code piece wise, ensuring every component of it works, so that when it breaks you can step back through the steps to see which one of you new code additions broke it. In this case... it's the isGrounded.
     
  5. Soumikbhat

    Soumikbhat

    Joined:
    Nov 23, 2013
    Posts:
    110
    For some reason the force is not working

    I simply used
    Code (JavaScript):
    1. function Update(){
    2.    
    3.         rigidbody.AddForce(Vector3.Up * -10);
    4.  
    5. }
    Don't know what' going wrong over here..as there's a rogidbody and character controller attached to the enemy character
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    Do you use rigidbodies?

    Is there a rigidbody attached to this gameobject?

    I'm looking at your original code. You don't move via a rigidbody to chase the player (actually I don't see ANY code to move towards the player seeing as what could be it is commented out.)
     
  7. Soumikbhat

    Soumikbhat

    Joined:
    Nov 23, 2013
    Posts:
    110
    Actually at first(when I wrote that original code), I did not think of using a rigidbody since I was going with a Character Controller.

    Now once I started facing this issue, I decided to use a rigidbody.

    An update by the way:

    I applied the original code (with the isgrounded boolean variable) and I discovered that the isgrounded never turns TRUE during runtime in the inspector window..
     
  8. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    I guess, you are using the component Animator with options enabled:

    -Apply Root Motion: If you use location keys in animations.
    -Animate Physics: Detect collisions. GameObject need collider and Rigidbody.

    When Component Animator apply root motion, don't apply gravity automatically.

    Animate Physics, not either apply gravity. Only detect collision.

    Solutions:

    -Raycast to the floor, and place on directly with transform.position.
    -You need animations with vertical movement or gravity.
    -Or, you can add layer 'Additive' with gravity animation displacement.
     
  9. Soumikbhat

    Soumikbhat

    Joined:
    Nov 23, 2013
    Posts:
    110
    Any idea as to why is

    Code (JavaScript):
    1. function Update(){
    2.  
    3.         rigidbody.AddForce(Vector3.Up * -10);
    4.  
    5. }
    not working? :(
     
  10. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    I was something wrong.

    Looking at Project Stealth animations, i have found another solution to apply gravity to animations, using Feet mode in Based Upon root transform position(Y).

    This would be the summary in pictures.

    Rotation Animations:




    Movement Animations:




    Jump Animations:




    And for rotations on irregular terrain, is necessary adjust a physic material with 0 friction.

     
  11. Soumikbhat

    Soumikbhat

    Joined:
    Nov 23, 2013
    Posts:
    110
    Thanks a lot, I shall try this out... :)