Search Unity

Adding gravity and collision to other Ai

Discussion in 'Scripting' started by jessee03, May 17, 2015.

  1. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    I've been trying a bunch of different ways to get my friendly ai to follow the player while using collision and gravity but the ai always ends up falling through the terrain. Navmesh is having a lot of issues for how I have my different areas setup, so I would like to set some kind of other path finding method. This is what I'm using currently for my friendly ai follow. I tried a lot of different things last night with gravity, character controllers, ect. but it always ended with the ai walking or falling through the terrain.

    Code (csharp):
    1.  
    2. float step2 = speed * Time.deltaTime;
    3. transform.position = Vector3.MoveTowards(transform.position,  new Vector3(user.transform.position.x,user.transform.position.y,user.transform.position.z), step2);
    4.  
     
  2. calmcarrots

    calmcarrots

    Joined:
    Mar 7, 2014
    Posts:
    654
    Tell me about your object. What components does it have? Tell me everything that it has. There could possibly be a much better solution than what you are doing.

    Also, this won't fix the problem but here is a more organized version of your code:

    Code (csharp):
    1. transform.position = Vector3.MoveTowards(transform.position, user.transform.position, speed * Time.deltaTime);
    Lastly, you might want to check out steering behavior algorithms. Specifically the seek algorithm and the obstacle avoidance one. (maybe a leader algorithm if you have multiple guys). Here is a link to a good starter algorithm:
    http://gamedevelopment.tutsplus.com/series/understanding-steering-behaviors--gamedev-12732

    Check out the other steering behaviors. They can add a lot of spice to your game AI!
     
  3. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    Thanks for that link! Right now I was trying to use a Character Controller on the friendly Ai in order to apply the gravity and collision. I feel that my code above might be messing with what I was originally trying to do. I've also attempted to work with the Simple movement function with the Character Controller but still had issues with terrain collision. So basically I'm just trying to get the Ai to follow the User while having gravity + terrain collision. I got the gravity working before for flat surfaces on the terrain but as soon as a hill was encountered the Ai fell through the terrain.
     
  4. calmcarrots

    calmcarrots

    Joined:
    Mar 7, 2014
    Posts:
    654
    Ok then in that case, you should use CharacterController.Move. You will have to create a direction vector to the player, then move the AI by that vector. Here is example code written in C#.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(CharacterController))]
    5. public class AI : MonoBehaviour
    6. {
    7.     public float speed;
    8.  
    9.     private GameObject player;
    10.     private CharacterController pCharControl;
    11.  
    12.     void Awake()
    13.     {
    14.         pCharControl = GetComponent<CharacterController>();
    15.         player = GameObject.FindGameObjectWithTag("Player");
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         Vector3 direction = (player.transform.position - transform.position).normalized;
    21.         direction *= speed;
    22.  
    23.         pCharControl.Move(direction * Time.deltaTime);
    24.     }
    25. }
    26.  
    Instead of using just a simple direction, you should move it using that seek algorithm. Anyways, if there are any errors or weird behaviors, let me know so that we can debug it together. Good luck :)
     
  5. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    Thank you I managed to get it working :) I did have to add some extra code to my update since I calculate for the Ai to stop moving about 30 units from my player, which caused the Ai to remain floating since the movement code is called depending on the ai state. So I had to add this to the Update to get the gravity to work properly. Works great now and no falling through the terrain!

    Code (csharp):
    1.  
    2. Vector3 direction2 = new Vector3(0, -10, 0).normalized;
    3. direction2 *= speed;
    4.  
    5. controller.Move(direction2 * Time.deltaTime);
    6.  
     
    calmcarrots likes this.