Search Unity

Question How to rotate an AI to the Player with planet like gravity

Discussion in 'Navigation' started by unity_D4D485FA782B2F756CF5, Mar 22, 2022.

  1. unity_D4D485FA782B2F756CF5

    unity_D4D485FA782B2F756CF5

    Joined:
    Sep 15, 2021
    Posts:
    3
    Hey guys, recently I have been working on a game that requires Mario Galaxy like enemy AI, meaning the enemy must be able to orbit around a sphere while following the player and rotating towards the player. We were able to figure out everything but the rotation towards the player, although I believe we are close, there are a few issues with the current code, such as the potato seeming to be constantly rotated upwards a slight bit, along with the speed at which the rotation occurs. Below I have included a video of the issue that will hopefully help to lead to a solution.

    Thanks for taking the time to read this post!



    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PotatoAI : MonoBehaviour
    7. {
    8.     public GameObject ground, follow;
    9.     public float GravityForce = 50;
    10.  
    11.     public float speed = 5;
    12.     private bool isFlat = false;
    13.     public bool followObj = true;
    14.     private Vector3 playerPos = new Vector3(0,0,0);
    15.  
    16.  
    17.  
    18.  
    19.     void Update()
    20.     {
    21.         if (followObj)
    22.         {
    23.             transform.position = Vector3.MoveTowards(transform.position, follow.GetComponent<Transform>().position, speed * Time.deltaTime);
    24.            
    25.             playerPos = follow.transform.position;
    26.  
    27.         }
    28.         if (!isFlat) {
    29.  
    30.             //Force down, calculated using the upwards force of the Ai and the gravity force
    31.             gameObject.GetComponent<Rigidbody>().AddForce((ground.transform.position - transform.position).normalized * GravityForce);
    32.             //Long equation that basically determains the way the Ai needs to turn and turns the AI
    33.  
    34.             //gameObject.transform.localRotation = Quaternion.Euler(0,follow.transform.rotation.y,0);
    35.  
    36.             // Rotation needed to stay on the ground
    37.             Vector3 groundRot = Quaternion.FromToRotation(transform.up, (transform.position + ground.transform.position).normalized) * transform.forward;
    38.            
    39.             //Finds the rotation needed to look at the player
    40.             Vector3 playerLook = (transform.position + playerPos).normalized + transform.forward;
    41.  
    42.             gameObject.GetComponent<Rigidbody>().MoveRotation(Quaternion.LookRotation(
    43.             /*Forward Vector*/ groundRot + playerLook
    44.             ,
    45.             /*Upwards position*/ (transform.position - ground.transform.position).normalized
    46.             ));
    47.         }
    48. }
    49.  
    50.  
    51.    
    52.     private void OnCollisionEnter(Collision collision)
    53.     {
    54.         if (collision.gameObject.CompareTag("Planet") && collision.gameObject != ground)
    55.         {
    56.             ground = collision.gameObject;
    57.         }
    58.         if (collision.gameObject.CompareTag("Platform") && collision.gameObject != ground)
    59.         {
    60.             isFlat = true;
    61.             ground = collision.gameObject;
    62.  
    63.         }
    64.     }
    65.     private void OnTriggerEnter(Collider other)
    66.     {
    67.         if (other.gameObject == follow)
    68.         {
    69.             followObj = true;
    70.         }
    71.        
    72.        
    73.        
    74.  
    75.     }
    76.  
    77.     private void OnTriggerExit(Collider other)
    78.     {
    79.         if (other.gameObject == follow)
    80.         {
    81.             followObj = false;
    82.         }
    83.  
    84.     }
    85. }
    86.  
    87.  
     
    Last edited: Mar 23, 2022
  2. RafaelGomes00

    RafaelGomes00

    Joined:
    Aug 13, 2020
    Posts:
    73
    I didnt understood your problem, you need that only the local rotation of the enemy rotates at the player, at the same time that it keeps withing it's orbit? If so, you can just use transform.LookAt(trasnform that should be looking at), it will make the enemy always look directly to the player.
     
  3. unity_D4D485FA782B2F756CF5

    unity_D4D485FA782B2F756CF5

    Joined:
    Sep 15, 2021
    Posts:
    3
    Yeah we tried using the .LookAt method but the enemy flips upside down when on the underside of the planet, ill see if I can post a video later today to clarify the issue as well
     
  4. unity_D4D485FA782B2F756CF5

    unity_D4D485FA782B2F756CF5

    Joined:
    Sep 15, 2021
    Posts:
    3
    I was able to update the post with some additional details and a video to hopefully clear up the confusion
     
  5. RafaelGomes00

    RafaelGomes00

    Joined:
    Aug 13, 2020
    Posts:
    73
    I see, I still think that transform.LookAt is your best option, as you said when the player is under the enemy it will make strange rotations, but what behaviour do you expect when the player is underneath the enemy? you can readjust the rotation after the lookat or something like that