敌人没有移动



在我的游戏中,我想让我的一个敌人向玩家移动,但他们出于某种原因没有这样做。敌人应该看着玩家,当玩家进入射程时,他们应该开始向他移动。我的问题是他们什么都没做。他们只是站在那里。而且,当它们有一个坚硬的身体时,它们甚至不会摔倒。它目前有一个Animator、长方体碰撞器和一个胶囊碰撞器。

编辑:我忘了添加这个,但脚本也触发了动画

编辑#2:我也知道这不是因为移动在if语句中

(如果不好的话,很抱歉,我是一个程序员noob(这是负责玩家运动的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mar_Tracker : MonoBehaviour
{
public Transform Player;
public float MoveSpeed = 3.0f;
public float InRadius = 250.0f;
public float AttackRange = 11.0f;
public float rocketRange = 50.0f;
private Animator anim;
private Coroutine RocketLouch = null;
public GameObject Rocket;
public GameObject Explosion;
public SphereCollider sphereCollider;
private void Start()
{
anim = GetComponent<Animator>();
Player = GameObject.FindGameObjectsWithTag("Player")[0].transform;
sphereCollider.enabled = false;
}
void Update()
{
Player = GameObject.FindGameObjectsWithTag("Player")[0].transform;
transform.LookAt(Player); // Makes it so that the enemy looks at player 
float dstSqr = (Player.position - transform.position).sqrMagnitude;
bool inRadius = (dstSqr <= InRadius * InRadius);
bool inAttackRange = (dstSqr <= AttackRange * AttackRange);
bool inRocketRange = (dstSqr <= rocketRange * rocketRange);
anim.SetBool("inArea", inRadius);
anim.SetBool("Attacking", inAttackRange);
if (inRadius)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime; // (movement)
}
if (inRocketRange)
{
if (RocketLouch == null)
{
RocketLouch = StartCoroutine(RocketLaunch());
}            
}
}
IEnumerator RocketLaunch()
{
anim.SetBool("Rocket", true);
yield return new WaitForSeconds(0.15f);
sphereCollider.enabled = true;
Explosion.SetActive(true);
yield return new WaitForSeconds(1.0f);
anim.SetBool("Rocket", false);
Destroy(Rocket);
}
}

对于你想要实现的目标来说,你使用的数学可能有点复杂,我认为问题就在那里。让我们简化它

Transform Player;
float InRadius;
float AttackRange;
float rocketRange;
void Start()
{
// Set it at the start to optimize performance
Player = GameObject.FindGameObjectsWithTag("Player")[0].transform;
}
// Use fixed Update for moving things around
// Better performance
void FixedUpdate()
{
HandlePlayerDetection() ;
}
void HandlePlayerDetection() 
{

transform.LookAt(Player); // Makes it so that the enemy looks at player 

// Find the distance between the player and the transform
var distance = Vector3.Distance(transform.position, player.position);

// Do the boolean calculations
bool inRadius = distance <= InRadius;
bool inAttackRange = distance <= AttackRange;
bool inRocketRange = distance <= rocketRange;

// Lets use inbuilt functions to movement
if (inRadius)
{
transform.position = Vector3.MoveTowards(transform.position, Player.position, MoveSpeed * Time.deltaTime);
}

// Rocket code
if (inRocketRange)
{
FireRocket();        
}
}

void FireRocket()
{
if (RocketLouch == null)
{
RocketLouch = StartCoroutine(RocketLaunch());
}   
}

现在,如果你真的想让你的敌人正常行走,我建议你看一个关于Nav Meshes的教程。它非常容易使用,可以让敌人在物体周围走动并进行道具路径查找。

最新更新