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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Does anyone know why these states aren't working?

Discussion in 'Scripting' started by cristo, May 19, 2015.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    I'm trying to get an AI character to descend on a Y axis and attack the player. By adding:
    Code (CSharp):
    1.     enemy.transform.position =  new Vector3 (52, 4, -178);
    the enemy now descends to the level of the player, and the enemy rotates to follow the player, but it doesn't approach the player.
    This part no longer works.
    Code (CSharp):
    1.     transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
    The chase and attack no longer work. The enemy is stuck at the new Vector3 (52, 4, -178);

    Does anyone know how the enemy can chase and attack the player after descending along Vector Y?


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class enemyAIScript : MonoBehaviour {
    5.    
    6.     public Transform player;
    7.     public float playerDistance;
    8.     public float rotationDamping;
    9.     public float moveSpeed;
    10.     public static bool isPlayerAlive = true;
    11.     //public float posY;
    12.  
    13.     public GameObject enemy;
    14.  
    15.    
    16.     // Use this for initialization
    17.     void Start () {
    18.        
    19.     }
    20.     void Awake ()
    21.     {
    22.         //posY = transform.position.y;
    23.        
    24.     }
    25.    
    26.     // Update is called once per frame
    27.     void Update () {
    28.  
    29.  
    30.         //if (isPlayerAlive) {
    31.             playerDistance = Vector3.Distance (player.position, transform.position);
    32.            
    33.             if (playerDistance < 96f) {
    34.                 diveAtPlayer ();
    35.             }
    36.  
    37.             if (playerDistance < 55f) {
    38.                 lookAtPlayer ();
    39.             }
    40.             if (playerDistance < 40f) {
    41.                 if (playerDistance > 2f) {
    42.                     chase ();
    43.                 } else if (playerDistance < 2f) {
    44.                     attack ();
    45.                 }
    46.             }
    47.         }
    48.     //}
    49.  
    50.     void diveAtPlayer()
    51.     {
    52.                 //float enemySpeed = 7.0f;
    53.                 //float totalSpeed = enemySpeed * Time.deltaTime;
    54.                 //transform.Translate (Vector3.down * totalSpeed);
    55.                 enemy.transform.position =  new Vector3 (52, 4, -178);
    56.                
    57.     }
    58.  
    59.  
    60.     void lookAtPlayer()
    61.     {
    62.  
    63.         Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
    64.         transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * rotationDamping);
    65.  
    66.     }
    67.     void chase ()
    68.     {
    69.        
    70.         transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
    71.        
    72.     }
    73.     void attack()
    74.     {
    75.         RaycastHit hit;
    76.         if(Physics.Raycast(transform.position, transform.forward, out hit))
    77.         {
    78.             if (hit.collider.gameObject.tag == "Player")
    79.             {
    80.                
    81.                
    82.                 //hit.collider.gameObject.GetComponent<healthScript>().health -= 5f;
    83.             }
    84.         }
    85.     }
    86. }
    87.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    diveAtPlayer() is being called each frame, so even if the rest of the code changes the position of the player it will be reset in the next frame.

    As per the previous thread, you need to handle the "mode" of the bird such that you can say

    pseudo
    Code (csharp):
    1.  
    2. if(atHeight and within swoop range)
    3. {
    4. swoop
    5. }
    6. if(!atHeight and within look range)
    7. {
    8. look at player
    9. }
    10.  
     
  3. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Thanks, I'll try to work off this.

     
  4. arty155

    arty155

    Joined:
    Apr 17, 2015
    Posts:
    22
    I have two questions about:
    Code (csharp):
    1.  
    2. void diveAtPlayer()
    3. {
    4.      enemy.transform.position =  new Vector3 (52, 4, -178);
    5. }
    6.  
    First, is there any reason to have to update all 3 positions instead of only updating the y axis?
    Second, you are hardcoding your height, what happens if the player ever changes their position along the y axis?

    I believe you can do the following, where "2" is the value above the player you want the enemy to hover at.
    Code (csharp):
    1.  
    2. void diveAtPlayer()
    3. {
    4.      enemy.transform.position.y = player.position.y + 2;
    5. }
    6.  
    This should accomplish a couple of things. First your enemy will continue to move as you originally coded it, now just along your hover height. Second your hover height will always be fixed at 2 units above the player, so even if the player runs up a slope the enemy will adjust. The value "2" is just a generic value, and is where I would adjust how high or low you want the enemy to hover around the player.
     
  5. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    @arty155 In case C# can't do that still, that syntax in C# form would be:

    Code (CSharp):
    1. enemy.transform.position = new Vector3(enemy.transform.position.x, player.position.y+2, enemy.transform.position.z);
     
  6. arty155

    arty155

    Joined:
    Apr 17, 2015
    Posts:
    22
    Thanks for the clarification, I'm still learning my C# syntax. However I believe Tomnnn's answer solves the OP's problem for getting the enemy to within player range along the y axis and still able to move.
     
  7. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Thanks for the feedback! I'll do my best to work this out and post results in a couple days.