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. Dismiss Notice

NPC sinking through ground while trying to follow player

Discussion in 'Scripting' started by Code1345, Jun 2, 2018.

  1. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    Hello! So I have a level in my game where an enemy is trying to chase after the player. I know this question has been asked a hundred times, but I haven't been able to use the information in those answers. What my issue is is, is that when my enemy chases after my character, he started to sink through the floor as he walks while tilting upwards, until he disappears. I am currently using a box collider on him, although I've used a rigidbody and charactercontroller up to this point, which didn't work. Here is my movement code below-
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityStandardAssets.Characters.FirstPerson;
    5. public class MovingScript : MonoBehaviour {
    6.     private Animator anim;
    7.     public GameObject player;
    8.     public float dist;
    9.     //active runs the corrutine
    10.     public bool active;
    11.     //deactive keeps the corrutine from rerunning
    12.     public bool deactive;
    13.     public float movespeed = 10f;
    14.     public Transform player2;
    15.     // Use this for initialization
    16.     void Start() {
    17.         active = true;
    18.         anim = GetComponent<Animator>();
    19.         player = GameObject.Find("FPSController");
    20.         deactive = false;
    21.         if (active == true)
    22.         {
    23.             StartCoroutine(Run());
    24.         }
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update() {
    29.         float dist = Vector3.Distance(player.transform.position, transform.position);
    30.          print("The player is:" + dist);
    31.      
    32.        
    33.         if (deactive == false)
    34.         {
    35.             anim.Play("CombatIdle");
    36.         }
    37.         if (deactive == true && dist >= 3)
    38.         {
    39.              transform.LookAt(player2.position);
    40.             transform.Translate(-transform.forward * movespeed * Time.deltaTime);
    41.        
    42.             anim.Play("Walk");
    43.          
    44.         }
    45.         else if (deactive == true && dist <= 3)
    46.         {
    47.             anim.Play("CombatIdle");
    48.         }
    49.  
    50.     }
    51.     IEnumerator Run()
    52.     {
    53.         //five seconds of dialogue
    54.         yield return new WaitForSeconds(5);
    55.         deactive = true;
    56.         active = false;
    57.     }
    58.  
    59.        
    60. }
    61.  
    62.  
    Any help would be appreciated!
     
  2. Defiled

    Defiled

    Joined:
    Feb 10, 2014
    Posts:
    43
    Code (CSharp):
    1. transform.Translate(-transform.forward * movespeed * Time.deltaTime);
    This ignores physics and just goes to that position, use a character Controller or a rigidbody if you want them to not go through objects.

    example:
    Code (CSharp):
    1. transform.GetComponent<CharacterController>().Move(-transform.forward * movespeed * Time.deltaTime);
     
    Lethn likes this.
  3. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    Thank you! That worked very well. My only issue is that the character collider causes the enemy to float upwards a few inches while it walks towards the player. Any advice on that? And again, thanks for your previous answer- I really appreciate it.
     
  4. Defiled

    Defiled

    Joined:
    Feb 10, 2014
    Posts:
    43
    I haven't tested this code but something like this would work
    Code (CSharp):
    1.         direction = -transform.forward;
    2.         if (controller.isGrounded)
    3.         {
    4.             gravity = -0.1f;
    5.         }
    6.  
    7.         gravity -= gravityStrength * Time.deltaTime;
    8.         controller.Move(new Vector3(direction.x, gravity, direction.y) * speed * Time.deltaTime);
    basically create ur own gravity
     
    Lethn likes this.