Search Unity

How to prevent enemy from falling over when suddenly stopped?

Discussion in 'Scripting' started by wcarbajal84, Oct 5, 2018.

  1. wcarbajal84

    wcarbajal84

    Joined:
    Aug 22, 2018
    Posts:
    4
    I have an enemy that when I get close to me he comes towards me and shoots me and when he gets to close he stops this is intended. Everything seems to be working fine but when he starts following and he stops he suddenly falls face down on the floor when he gets to close then as I walk away he gets back up. Also, when he follows and I back up away from him he falls and then gets back up when I get close to him.

    My first time posting on unity forums so I'm unsure if this the correct place to post.

    Below is my code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class Shooting_Enemy : MonoBehaviour
    8. {
    9.     private Transform _player;
    10.  
    11.     private float _distance;
    12.  
    13.     [SerializeField] private GameObject _EnemyBulletPrefab;
    14.     private GameObject _EnemyBullet;
    15.  
    16.     private bool _alive;
    17.  
    18.  
    19.     private bool _playerInRange;
    20.     void Start()
    21.     {
    22.         _player = GameObject.Find("Player").transform;
    23.         _alive = true;
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         CheckDistance();
    29.         if (_playerInRange)
    30.             AttackPlayer();
    31.  
    32.  
    33.  
    34.  
    35.  
    36.     }
    37.  
    38.     private void CheckDistance()
    39.     {
    40.         _distance = Vector3.Distance(this.transform.position, _player.transform.position);
    41.         if (_distance > 3 && _distance <= 10)
    42.         {
    43.  
    44.             _playerInRange = true;
    45.         }
    46.  
    47.         else
    48.         {
    49.             _playerInRange = false;
    50.         }
    51.     }
    52.  
    53.  
    54.     private void AttackPlayer()
    55.     {
    56.         //Turning enemy to look at player
    57.         transform.LookAt(_player);
    58.         transform.Translate(0, 0, Time.deltaTime);
    59.  
    60.         //Shoot Player if enemy is still alive
    61.         if (_alive)
    62.         {
    63.  
    64.             Ray ray = new Ray(transform.position, transform.forward);
    65.             RaycastHit hit;
    66.             if (Physics.SphereCast(ray, 0.75f, out hit))
    67.             {
    68.                 GameObject hitObject = hit.transform.gameObject;
    69.                 if (hitObject.GetComponent<PlayerCharacter>())
    70.                 {
    71.                     if (_EnemyBullet == null)
    72.                     {
    73.                         _EnemyBullet = Instantiate(_EnemyBulletPrefab) as GameObject;
    74.                         _EnemyBullet.transform.position = transform.TransformPoint(Vector3.forward * 1.5f);
    75.                         _EnemyBullet.transform.rotation = transform.rotation;
    76.                     }
    77.                 }
    78.  
    79.             }
    80.         }
    81.     }
    82.  
    83.  
    84. }
    85.  
     

    Attached Files:

  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    wcarbajal84 likes this.
  3. wcarbajal84

    wcarbajal84

    Joined:
    Aug 22, 2018
    Posts:
    4
    I messed around with Rigidbody constrains like you suggested and got it working. Thanks Joe-Censored
     
    Joe-Censored likes this.