互联网的向导,
我正在制作3D第一人称角色扮演游戏,你可以攻击位于地图周围的敌人。我目前的剧本工作正常,但是有一个问题:只要他们专注于玩家,敌人就会穿过墙壁。
我已经尝试了一些方法来解决这个问题,但不幸的是没有结果。有谁知道这个问题的解决方案?
提前致谢。
goblinAttack.cs:
using UnityEngine;
using System.Collections;
public class goblinAttack : MonoBehaviour {
public Transform player;
static Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
Vector3 direction = player.position - this.transform.position;
float angle = Vector3.Angle(direction,this.transform.forward);
if(Vector3.Distance(player.position, this.transform.position) < 10 && angle < 30)
{
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("isIdle",false);
if(direction.magnitude > 5)
{
this.transform.Translate(0,0,0.10f);
anim.SetBool("isRunning",true);
anim.SetBool("isAttacking",false);
anim.SetBool("isCrying",true);
}
else
{
anim.SetBool("isAttacking",true);
anim.SetBool("isRunning",false);
anim.SetBool("isCrying",false);
}
}
else
{
anim.SetBool("isIdle", true);
anim.SetBool("isRunning", false);
anim.SetBool("isAttacking", false);
anim.SetBool("isCrying",false);
}
}
}
NPC设置:
答案 0 :(得分:1)
标有IsKinematic的刚体is not affected by Forces, collisions or joints.请记住,使用带有刚体的Transform.Translate()可能也会导致错误 - 请考虑Rigidbody.MovePosition()用于物理学。但总的来说,我建议您使用pathfinding来确保敌人不会首先尝试穿过墙壁。
答案 1 :(得分:0)
你可以试试这个:
using UnityEngine;
using System.Collections;
public class walkTowardsPlayer : MonoBehaviour {
List<GameObject> enemies = new List<GameObject>();
public GameObject player; //drag your player in inspector to this variable
public GameObject zombie; //drag your enemy in inspector to this variable
void Start(){
}
void Update(){
foreach(GameObject exampleZombie in enemies){
if (zombiechar.transform.position!=player.transform.position){
zombiechar.transform.position = new Vector3(zombiechar.transform.position.x, 0f, zombiechar.transform.position.z); //enemy move only at x and z axes
zombiechar.transform.position = Vector3.MoveTowards(zombiechar.transform.position, player.transform.position, 1.5f*Time.deltaTime); //the enemy move towards you
}
zombie = exampleZombie;
}
}
}