我一直试图找到解决我的问题的方法,这个问题涉及我的场景中的游戏对象,它穿过高架地形和岩石。岩石有碰撞器,敌人物体有对撞机和刚体。我附上了我的脚本,当FPS达到一定范围时,它会移动敌人。这一切都很好,但它穿过岩石和高架地形。到目前为止,我已经尝试了许多建议但似乎没有任何效果。
using UnityEngine;
using System.Collections;
public class CreatureWalk : MonoBehaviour {
public int totalSecs = 0;
public GameObject explosion;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
GameObject GM = GameObject.Find ("GameManager");
//Get Game Manager Component on Object
GameManager GM_Component = GM.GetComponent<GameManager> ();
if (GM_Component.GameOver) {
animation.Play ("idle");
} else if (GM_Component.FlowerHits > 30) {
while (totalSecs < 5000) {
animation.Play ("hit");
totalSecs += 1;
}
//Destroy (gameObject);
//create explosion
Instantiate (explosion, transform.position, transform.rotation);
Invoke("Disable",3);
Invoke("Reenable",3);
Vector3 currentPosition = this.transform.position; //get current position of creature
transform.position = new Vector3(currentPosition.x+ Random.Range(-20.0F, 20.0F),0.2f,currentPosition.z +Random.Range(-20.0F, 20.0F));
totalSecs =0;
GM_Component.FlowerHits =0;
} else {
animation.Play ("walk");
MoveTowardsTarget ();
}
}
private void Disable() {
gameObject.SetActive(false);
//Debug.Log ("Disable");
}
private void Reenable() {
gameObject.SetActive(true);
//Debug.Log ("Re-enable");
}
//move towards a target at a set speed.
private void MoveTowardsTarget() {
//the speed, in units per second, we want to move towards the target
float speed = 1;
//move towards the fps
Vector3 targetPosition = new Vector3(Camera.main.transform.position.x,0,Camera.main.transform.position.z);
Vector3 currentPosition = this.transform.position;
transform.LookAt (targetPosition);
//first, check to see if we're close enough to the target
if (Vector3.Distance (currentPosition, targetPosition) < 30f) {
Vector3 directionOfTravel = targetPosition - currentPosition;
directionOfTravel.Normalize ();
this.transform.Translate (
(directionOfTravel.x * speed * Time.deltaTime),
(directionOfTravel.y * speed * Time.deltaTime),
(directionOfTravel.z * speed * Time.deltaTime),
Space.World);
//this.rigidbody.MovePosition(directionOfTravel);
// (directionOfTravel.x * speed * Time.deltaTime),
// (directionOfTravel.y * speed * Time.deltaTime),
// (directionOfTravel.z * speed * Time.deltaTime));
//this.rigidbody.AddForce(
// (directionOfTravel.x * speed * Time.deltaTime),
// (directionOfTravel.y * speed * Time.deltaTime),
// (directionOfTravel.z * speed * Time.deltaTime),ForceMode.VelocityChange);
//rigidbody.AddForce(transform.forward * 10);
} else
animation.Play ("idle");
}
}
我已尝试使用AddForce移动,因为您可以通过注释代码看到,但敌人只是走了一步。
该生物有一个刚体,重力和运动学检查。
答案 0 :(得分:0)
尝试从它的外观添加1000或10000力...你使用1的分数...如果你的质量为1,它仍然需要超过1的力才能使它移动。如果你增加1000力并且他的质量是1并且他仍然没有移动,那么确保他没有在他的刚体中设置为运动学。
如果你仍然没有动,那么就会发生更加阴险的事情。我建议检查你是否正确使用了正确的轴,正确的方向等等......那些不可能出错的事情。
祝你好运,希望有所帮助。答案 1 :(得分:0)
每当您使用AddForce时,请尝试将方向向量乘以您想要的速度和对象的Rigidbody质量。另外,取消选中&#34; isKinematic&#34;复选框,因为那种&#34;停用&#34;物理引擎对该对象的计算。如果你想要准确,你也可以手动设置物体的刚体的速度。