- Home /
Tank not moving towards player
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<UnityEngine.AI.NavMeshAgent>();
// 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
Answer by SohailBukhari · May 30, 2018 at 04:52 AM
Your code is fine. i think you declare the boolean playerInRange
is not true in the inspector, thats why your NavmeshAgent
is not moving.
Make Sure playerInRange is checked in the inspector.
other thing is that you don't need the true check in the condition if (playerInRange == true)
, its true you can just write.
if (playerInRange)
EDIT 1 : If that not move than another Update is blocking the Navmesh agent to move. Anywhere in the other script your playerInRange
is false thats why Navmesh sits.
Note: Navmesh work only on Baked Areas which are marked as static.
Hi, thank you, my scene is set up so that when the player gets close enough to the tank it becomes "in range", just to make sure I tried checking it. This had no effect though. The tank still just sits in the ground looking at the player.
$$anonymous$$y Nav $$anonymous$$esh Agent:
I have also made sure to bake it.
Thanks
Where you setup player radius? the script you shared not defined anywhere the radius, the tank will move from anywhere without any radius calculation towards the player. playerInRange is true always so your tank will always move. If that not move than another Update
is blocking the Navmesh agent to move. Anywhere in the other script your playerInRange
is false thats why Navmesh sits.
I don't really understand Radius, but my "Agent Radius" in the bake section is set to 2? Didn't change anything when I altered this though. I've looked everywhere and made sure that playerInRange is set to true everywhere - no results. I cant find the problem anywhere, this is really bad :(. Thanks for the advice though
Your answer
Follow this Question
Related Questions
Nav Mesh Agent not smooth 1 Answer
NavMesh problem it does not respond corectly 0 Answers
Enemy follow Camera 0 Answers
NavMeshAgent Collision 1 Answer
No player is colliding with enemy even with capsule collider !!! Please me help me. 0 Answers