【发布时间】:2018-04-25 19:46:49
【问题描述】:
我有一个玩家对象和一个名为 StartRamp 的坡道。我正在使用下面的代码来改变我的球员的位置和旋转到斜坡外的表面。当我的玩家离开坡道时,我想将玩家在 x 轴上的旋转更改为仅在 50,-50 之间。
但是当表面为假时我的 else 语句没有运行。这让我觉得我的 OnCollisionExit() 是错误的。有人可以帮我吗?
具有以下代码的脚本附加到播放器对象
private void OnCollisionStay(Collision collision)
{
if(collision.gameObject.name == "StartRamp"){
surface = true;
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.name != "StartRamp")
{
surface = false;
}
}
public void playerToSurface(){
if (surface == true){ // Change Position When On Ramp
Ray ray = new Ray(transform.position, -transform.up);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1.5f) == true)
{
Debug.DrawLine(transform.position, hit.point, Color.green);
rotCur = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
posCur = new Vector3(transform.position.x, hit.point.y, transform.position.z);
grounded = true;
}
else
{
grounded = false;
}
if (grounded == true)
{
transform.rotation = Quaternion.Lerp(transform.rotation, rotCur, Time.deltaTime * 5);
}
else
{
transform.position = Vector3.Lerp(transform.position, transform.position - Vector3.up * 1f, Time.deltaTime * 5);
}
}else{ // Change Position When Off Ramp
Debug.Log("Player is off ramp.\n");
}
}
// Update player position to ground
private void Update()
{
playerToSurface();
}
【问题讨论】:
-
您不应该在退出时检查名称
== "StartRamp"吗?你有!=
标签: unity3d rotation position collision-detection raycasting