【发布时间】:2017-09-15 22:18:44
【问题描述】:
所以基本上问题是当我想旋转角色并移动它时,移动不会像预期的那样正常工作。如果我的 y 坐标低于零,则角色向后移动而不是向前移动。所以基本上是相反的方向。此外,如果我的操纵杆坐标例如:y=0,23 和 x=0,8 我的角色显示在那个方向但向右下方移动.. PS:y和x值是从0到1(单位圆) 但是,如果我只移动角色而不旋转他,那么角色的移动就会正常运行。
这是我当前的代码:
void Update()
{
if (myX != leftController.GetTouchPosition.x || myY != leftController.GetTouchPosition.y) { //checks if player changed position.
myX = leftController.GetTouchPosition.x;
myY = leftController.GetTouchPosition.y;
double rad = Mathf.Atan2(leftController.GetTouchPosition.y, leftController.GetTouchPosition.x); // In radians
double deg = rad * (180 / System.Math.PI); // values from up right to up left : +0 to +180 and from down left to down right: -180 to -0
double myRotAngle = 0;
//calculating the specific angle in which the character should move
if (deg < 0) {
myRotAngle = 90 + ((deg * -1));
} else if (deg >= 0 && deg <= 90)
myRotAngle = 90 - deg;
else if (deg >= 90 && deg <= 180)
myRotAngle = 270 + (180 - deg);
transform.localEulerAngles = new Vector3 (0f,(float)myRotAngle, 0f);//rotate
}
_rigidbody.MovePosition(transform.position + (transform.forward * leftController.GetTouchPosition.y * Time.deltaTime * speedMovements) +
(transform.right * leftController.GetTouchPosition.x * Time.deltaTime * speedMovements) );//move
}
【问题讨论】:
-
以及MovePosition,刚体有一个MoveRotation方法,你应该看看
-
@PierreBaret 之前已经这样做了。结果不变
-
为什么不使用变换而不是刚体移动?
-
@PierreBaret 因为结果与我使用 transform 或 rigdidbody 的天气相同