添加凸轮碰撞脚本

时间:2019-07-14 16:35:24

标签: c# unity3d game-development

我已经合作了几天,但遇到的问题是我不确定如何为当前功能添加“凸轮碰撞”。所以问题是,有人可以帮助我吗?那就太好了!

Vace:)

[编辑]: 到目前为止,我已经添加了我最有效的版本。现在,我遇到的问题是,如果我将鼠标移到某个物体上的速度太快,我将忽略“命中物体”,并且摄像机会穿过墙壁。这个可以解决吗?

public float mouseSensitivity = 4;
public Transform target;
public float dstFromTarget = 4;
public Vector2 pitchMinMax = new Vector2(-70, 85);

private float curZoom = 6f;
public float zoomSmoothTime = 0.1f;
float zoomSmoothVelocity;

float yaw;
float pitch;

public float minDst = 2f;
public float maxDst = 12f;

void LateUpdate()
{
    Rotate();
    Zoom();
}

private void Rotate()
{
    yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
    pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
    pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y);

    transform.eulerAngles = new Vector3(pitch, yaw);
}

private void Zoom()
{
    // out
    if (Input.GetAxis("Mouse ScrollWheel") < 0)
    {
        dstFromTarget++;
    }

    //in
    if (Input.GetAxis("Mouse ScrollWheel") > 0)
    {
        dstFromTarget--;
    }
    dstFromTarget = Mathf.Clamp(dstFromTarget, minDst, maxDst);

    Ray ray = new Ray(transform.position, -transform.forward);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit, dstFromTarget))
    {
        curZoom = Mathf.SmoothDamp(curZoom, hit.distance, ref zoomSmoothVelocity, zoomSmoothTime);
    }
    else
    {
        curZoom = Mathf.SmoothDamp(curZoom, dstFromTarget, ref zoomSmoothVelocity, zoomSmoothTime);
    }
    curZoom = Mathf.Clamp(curZoom, minDst, maxDst);
    transform.position = target.position - transform.forward * curZoom;
}

0 个答案:

没有答案