倾斜到目标位置,但仅在半径范围内

时间:2019-02-12 21:32:32

标签: unity3d

我正在制作3D游戏,但是我的玩家只能在X和Y轴上移动。我的鼠标后面有一个附带摄像机的播放器,但是即使我的鼠标超出了这些范围,我也只希望它跟随与Vector3.zero的最大半径距离。

我尝试将播放器重新定位到半径,使其试图在超出其边界的每个帧上跟随半径移动最大距离,但这即使在LateUpdate中也会导致照相机抖动。

void LateUpdate()
{
    if (Input.GetMouseButtonDown(0)) {
        firstTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
        playerPos = transform.position;
    }

    if (Input.GetMouseButton(0)) {
        Ray currentTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
        Vector2 direction = currentTouchPos.origin - firstTouchPos.origin;
        float distance = Vector3.Distance(transform.position, Vector3.zero);

        if (distance >= radius) {
            targetPosition = direction.normalized * radius;
        } else {
            targetPosition = playerPos + direction * touchSensitivity;
        }

    }

    transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * followSpeed);
}

我在想,必须有一种方法可以将播放器的位置固定在一个半径上,这样我就不必通过在每一帧通过代码来重新定位播放器来“束缚”他。

2 个答案:

答案 0 :(得分:2)

您应该尝试使用Vector3.ClampMagnitude()

答案 1 :(得分:1)

解决方案是使用钳制幅度。

var Profile = require('./profile');

var Helper = Profile.discriminator('helper', new mongoose.Schema({
    jobTitle: { type: String, default: "" },
    lastPosition: { type: String, default: "" }
}));

module.exports = mongoose.model("Helper");