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

Enemy chases any object with player tag (no navmesh)

Discussion in 'Scripting' started by rudigreig, Oct 10, 2020.

  1. rudigreig

    rudigreig

    Joined:
    Dec 11, 2018
    Posts:
    50
    I can't use navmesh agent because my game is an infinite runner, im stuck on how to find gameobjects with tag
     
  2. rudigreig

    rudigreig

    Joined:
    Dec 11, 2018
    Posts:
    50
    came up with this script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CHASE : MonoBehaviour {
    6.  
    7.     private Transform target;
    8.     public float speed = 1.0f;
    9.  
    10.     private void Start()
    11.     {
    12.         target = GameObject.FindWithTag("Player").transform;
    13.  
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         float step = speed * Time.deltaTime;
    20.         transform.LookAt(target);
    21.         transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    22.     }
    23. }
    24.  
    I have rigidbody constraints and a collider but the object moves in any rotation and direction and it clips through objects, how do I fix this?