我的团结2D游戏(口袋妖怪风格)存在问题,我使用transform.position
来移动游戏对象。
我有一个跟随他的球员和敌人,一切都好。但是当敌人发生碰撞时,他们会开始相互推动
当敌人和玩家发生碰撞时,我需要无人推动。
OnCollision
检测代码中的碰撞,但是当我取消敌人的动作时,他们就不会再回来了。---- ---- UPDATE
我需要碰撞,但没有在它们之间推动,这是一个视频来说明问题 https://www.youtube.com/watch?v=VkgnV1NOxlw
为了记录,我在这里使用A *寻路脚本(http://arongranberg.com/astar/),我的敌人会移动脚本。
void FixedUpdate () {
if(path == null)
return;
if(currentWayPoint >= path.vectorPath.Count)
return;
Vector3 wayPoint = path.vectorPath [currentWayPoint];
wayPoint.z = transform.position.z;
transform.position = Vector3.MoveTowards (transform.position, wayPoint, Time.deltaTime * speed);
float distance = Vector3.Distance (transform.position, wayPoint);
if(distance == 0){
currentWayPoint++;
}
}
---- ---- UPDATE
最后,我得到预期的结果,当目标关闭时将rigidbody2D.isKinematic属性更改为true并停止
以下是视频https://www.youtube.com/watch?v=0Zm0idUU75s
敌人的移动代码
void FixedUpdate () {
if(path == null)
return;
if(currentWayPoint >= path.vectorPath.Count)
return;
float distanceTarget = Vector3.Distance (transform.position, target.position);
if (distanceTarget <= 1.5f) {
rigidbody2D.isKinematic = true;
return;
}else{
rigidbody2D.isKinematic = false;
}
Vector3 wayPoint = path.vectorPath [currentWayPoint];
wayPoint.z = transform.position.z;
transform.position = Vector3.MoveTowards (transform.position, wayPoint, Time.deltaTime * speed);
float distance = Vector3.Distance (transform.position, wayPoint);
if(distance == 0){
currentWayPoint++;
}
}
答案 0 :(得分:3)
您可以通过多种方式实现这一目标,
您可以使用Physics2D.IgnoreCollision
Physics2D.IgnoreCollision(someGameObject.collider2D, collider2D);
IgnoreCollision
调用,可能是在对象实例化时。或者您也可以使用Layer Collision Matrix
GameObjects
来避免碰撞。尝试:
Edit->Project Settings->Physics
或者,如果您希望它停止移动,您可以轻松地执行此操作,
bool isCollided = false;
// when when OnCollisionEnter() is called stop moving.
//maybe write your move script like
void Move() {
if(!isCollided) {
// move logic
}
}