Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Floating enemies?

Discussion in 'Scripting' started by ecloev, Oct 13, 2015.

  1. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Hello,
    My enemies are floating above the ground? How can I get them to walk like attached tog round?
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class AdvancedAI : MonoBehaviour
    6. {
    7.     float distance;
    8.    
    9.     public float lookAtDistance = 25.0F;
    10.     public float chaseRange = 2050.0F;
    11.     public float attackRange =4.5F ;
    12.     public float moveSpeed = 5.0F;
    13.     public float damping = 6.0F;
    14.     public float damage = 30.0F;
    15.     public float attackRepeatTime = .8F;
    16.     public Transform target;
    17.     public CharacterController controller;
    18.     private float verticalMomentum = 0f;
    19.     private float gravity = -9.8f;
    20.    
    21.     private Vector3 moveDirection = Vector3.zero;
    22.     private float attackTime;
    23.  
    24.  
    25.  
    26.     //TODO: add in a function to find controller and to locate and assign the player as the target
    27.    
    28.     void Start()
    29.     {
    30.         attackTime = Time.time;
    31.     }
    32.    
    33.     void Update()
    34.     {
    35.         distance = Vector3.Distance(target.position, transform.position);
    36.         verticalMomentum += gravity * Time.deltaTime;
    37.        
    38.         if(distance < lookAtDistance)
    39.         {
    40.             LookAt();
    41.         }
    42.        
    43.         if (distance < attackRange)
    44.         {
    45.             AttackPlayer();
    46.         }
    47.        
    48.         else if (distance < chaseRange)
    49.         {
    50.             ChasePlayer();
    51.  
    52.         }
    53.     }
    54.    
    55.     void LookAt()
    56.     {
    57.         Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
    58.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    59.     }
    60.    
    61.     void ChasePlayer()
    62.     {
    63.         moveDirection = transform.forward;
    64.         moveDirection *= moveSpeed;
    65.         moveDirection.y += verticalMomentum * Time.deltaTime;
    66.         controller.Move(moveDirection * Time.deltaTime);
    67.     }
    68.    
    69.     void AttackPlayer()
    70.     {
    71.         //TODO: Need Attack Animations
    72.         if (Time.time > attackTime)
    73.         {
    74.             target.SendMessage("damagePlayer", damage, SendMessageOptions.DontRequireReceiver);
    75.             attackTime = Time.time + attackRepeatTime;
    76.         }
    77.     }
    78.    
    79.     void ApplyDamage()
    80.     {
    81.         chaseRange += 15;
    82.         moveSpeed += 1;
    83.         lookAtDistance += 20;
    84.     }
    85. }
    86.  
     
  2. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    Sorry, this is not my field of expertise, but you are changing verticalMomentum twice (once in Update and again when calling your function) and also should you not be checking if the controller is grounded when doing those calculations?
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you're also multiplying by time.deltaTime twice, lines 36 and 65, so it's going to be tiny once applied
     
  4. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    you are just repeating what I said..."changing verticalMomentum twice"
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    "changing" isn't quite the same as multiplying by 0.0001 twice... but ok.
     
  6. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    granted...