Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Tank not moving towards player

$
0
0
HI, I'm pretty new to Unity and have just started trying to use NavMeshAgent. i'm trying to make my enemy (A tank) move towards the player, but at the moment it kinda just sits then until it is hit and slides away. I think that means that my NavMesh is alright but my script is off? any help would be great thanks :) using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; namespace BiddiscombeOlsson { public class SoldierControl : MonoBehaviour { // PUBLIC VARIABLES public bool playerInRange; public float thrust = 200f; public float dampening = 2.5f; void Update() { // If the player is in range .. if (playerInRange == true) { // .. find the player's position Transform targetLocation = GameObject.FindWithTag("Player").transform; // Set the reference to the soldier's navMeshAgent component UnityEngine.AI.NavMeshAgent navAgent = gameObject.GetComponent(); // Set the destination for the navAgent navAgent.SetDestination(targetLocation.position); // This makes the turret look at the player's X and Z but ignores the Y Vector3 playerPosition = new Vector3(targetLocation.position.x, transform.position.y, targetLocation.position.z); Vector3 aim = playerPosition - transform.position; Quaternion rotate = Quaternion.LookRotation(aim); // Lerp rotation and include the dampening to ease motion transform.rotation = Quaternion.Lerp(transform.rotation, rotate, Time.deltaTime * dampening); } } } } Thanks, can post other information if needed

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>