我有一个在平台上移动的对象(不仅在顶部而且在所有侧面)。我正在使用OnTriggerExit2D进行旋转,并且每次碰撞停止时我的对象都会在z轴上旋转90度,但是由于它一旦离开平台就旋转了,所以我不希望对象和平台之间的距离在旋转时发生。你能告诉我为什么会这样吗?另外,我将rotate
变量设置为270以旋转90度。当我将其设置为90时,它将旋转270度。也许这是对的,但我不确定,这感觉很奇怪。这是源代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Platformer : MonoBehaviour
{
[Header("Run variables")]
public float enemyMove = 1f;
private float rotate = 270f;
private int whereIsFace = 0;
Rigidbody2D myRigidBody;
Transform myTransform;
void Start()
{
myRigidBody = GetComponent<Rigidbody2D>();
myTransform = GetComponent<Transform>();
}
void Update()
{
switch(whereIsFace)
{
case 0:
myRigidBody.velocity = new Vector2(enemyMove, 0f);
break;
case 1:
myRigidBody.velocity = new Vector2(0f, -enemyMove);
break;
case 2:
myRigidBody.velocity = new Vector2(-enemyMove, 0f);
break;
case 3:
myRigidBody.velocity = new Vector2(0f,enemyMove);
break;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
whereIsFace++;
if (whereIsFace == 4)
whereIsFace = 0;
if (rotate == 360f)
rotate = 0f;
transform.localRotation = Quaternion.Euler(0, 0, rotate);
rotate -= 90;
}
}
}
我将精灵及其对撞机连接到平台的左上方。当我按下游戏键时,我的物体转过平台的一半it is looking like this您可以看到第二个盒子碰撞器对于OnTriggerExit2D来说更薄(像一根棍子)。