Unity 3D机芯和相机控制器

时间:2018-02-27 13:25:28

标签: c# unity3d animation scripting

现在我有一个附有动作脚本的第一人称角色。我使用刚体和角色控制器。相机使用播放器对象控制父对象的转换,在本例中为胶囊。

玩家对象上有移动脚本,相机上有一个摄像头控制器,只允许鼠标控制玩家对象的转换。

当我跳跃时,我的脚本会这样,玩家保持动量,并且不能停止空中,但如果我转动相机,玩家对象也会根据转换改变轨迹。如果我看起来正确,在半空中,我的跳跃也会跟随相机向右移动。我希望在半空中进行跳跃动作,以保持原始轨迹。有人可以帮助我添加该功能,但相机仍然可以环顾四周?

我的动作脚本:

moveVector = lastMove;

它的:

public class CameraController : MonoBehaviour {

    Vector2 mouseLook;
    Vector2 smoothV;
    public float sensitivity = 5.0f;
    public float smoothing = 2.0f;

    GameObject character;

    // Use this for initialization
    void Start () {
        character = this.transform.parent.gameObject;
    }

    // Update is called once per frame
    void Update () {
        var md = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));

        md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));
        smoothV.x = Mathf.Lerp (smoothV.x, md.x, 1f / smoothing);
        smoothV.y = Mathf.Lerp (smoothV.y, md.y, 1f / smoothing);
        mouseLook += smoothV;

        //Låser kameraret så man ikke kan kigge længere ned eller op end 90 grader
        mouseLook.y = Mathf.Clamp (mouseLook.y, -90f, 90f);

        transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, Vector3.right);
        character.transform.localRotation = Quaternion.AngleAxis (mouseLook.x, character.transform.up);
    }
}

这样做我的玩家不会停止半空。

如果有可能这样做你也无法改变空中方向,但你可以稍微拖动一下轨迹,但是顺利地,就像在反击中一样:全球攻势等等。

更新: 这是我的相机代码:

CREATE TABLE words_scores (
        mid     bigint  NOT NULL REFERENCES words_moves ON DELETE CASCADE,
        gid     integer NOT NULL REFERENCES words_games ON DELETE CASCADE,
        uid     integer NOT NULL REFERENCES words_users ON DELETE CASCADE,
        word    text    NOT NULL CHECK(word ~ '^[A-Z]{2,}$'),
        score   integer NOT NULL CHECK(score >= 0)
);

2 个答案:

答案 0 :(得分:0)

你似乎把镜头的方向混淆了。在第一人称中,这对于“玩家”来说是常见的。将位置移动到与摄像机相同的方向,但它必须是这样的。相机方向由鼠标控制,但您使用相机方向来设置行进方向。

您需要单独存储当前的Direction Vector并根据某种算法(例如双曲线)更改Direction Vector,并在玩家Jump期间忽略Camera Orientation。您可以使用“摄影机方向”更改“视图矩阵”,但不能在飞行中使用相机方向更改摄像机位置。

答案 1 :(得分:0)

以下是我用于矿山项目的旁观相机控制器的代码部分,希望它能为您提供帮助。

void Update()
{
    CameraMovementValidation();
    CameraRotationValidation();
    Camerazoom();

    if (Input.GetKey(KeyCode.LeftShift))
    {
        doSlowMotion = true;
    }
    else if (Input.GetKeyUp(KeyCode.LeftShift))
    {
        doSlowMotion = false;
    }
}

public void FixedUpdate()
{
    CameraMovement();
    CameraRotation();
}

public void LateUpdate()
{
    Controller();
}

private void CameraMovementValidation()
{
    if (Input.GetKey(KeyCode.W))
    {
        doCameraUpDownMove = true;
        cameraUpDownMoveDirection = 1;
    }
    else if (Input.GetKey(KeyCode.S))
    {
        doCameraUpDownMove = true;
        cameraUpDownMoveDirection = -1;
    }
    else
    {
        doCameraUpDownMove = false;
    }

    if (Input.GetKey(KeyCode.A))
    {
        doCameraLeftRightMove = true;
        cameraLeftRightDirection = -1;
    }
    else if (Input.GetKey(KeyCode.D))
    {
        doCameraLeftRightMove = true;
        cameraLeftRightDirection = 1;
    }
    else
    {
        doCameraLeftRightMove = false;
    }

    if (Input.GetKey(KeyCode.Q))
    {
        doCameraForwardBackMove = true;
        cameraForwardBackDirection = 1;
    }
    else if (Input.GetKey(KeyCode.E))
    {
        doCameraForwardBackMove = true;
        cameraForwardBackDirection = -1;
    }
    else
    {
        doCameraForwardBackMove = false;
    }
}

private void CameraMovement()
{
    if (doCameraUpDownMove)
    {
        cameraPosition += transform.rotation * Vector3.up * moveCoefficent * cameraUpDownMoveDirection * camereSlowMotionMoveCoefficient;
    }

    if (doCameraLeftRightMove)
    {
        cameraPosition += transform.rotation * Vector3.right * moveCoefficent * cameraLeftRightDirection * camereSlowMotionMoveCoefficient;
    }

    if (doCameraForwardBackMove)
    {
        cameraPosition += transform.rotation * Vector3.forward * moveCoefficent * cameraForwardBackDirection * camereSlowMotionMoveCoefficient;
    }
}

private void CameraRotationValidation()
{
    if (Input.GetMouseButtonDown(1))
    {
        mouseXUpdateForRotation = Input.mousePosition.x;
        mouseYUpdateForRotation = Input.mousePosition.y;
    }

    if (Input.GetMouseButton(1))
    {
        doCameraRotation = true;
    }
    else
    {
        doCameraRotation = false;
    }
}

private void CameraRotation()
{
    if (doCameraRotation)
    {
        if (Input.mousePosition.x != mouseXUpdateForRotation)
        {
            cameraRotationX += (Input.mousePosition.x - mouseXUpdateForRotation) * rotationFactor;
            mouseXUpdateForRotation = Input.mousePosition.x;
        }

        if (Input.mousePosition.y != mouseYUpdateForRotation)
        {
            cameraRotationY += (Input.mousePosition.y - mouseYUpdateForRotation) * rotationFactor;
            mouseYUpdateForRotation = Input.mousePosition.y;
        }
    }
}

private void Camerazoom()
{
    mouseScrollCoefficient = Input.GetAxis("Mouse ScrollWheel");
    if (mouseScrollCoefficient > 0)
    {
        zoom += mouseScrollCoefficient * -cameraZoomCoefficent;
    }
    else if (mouseScrollCoefficient < 0)
    {
        zoom += mouseScrollCoefficient * -cameraZoomCoefficent;
    }
}

private void Controller()
{
    transform.position = Vector3.Lerp(transform.position, cameraPosition, moveSmoothlyCoefficient);
    transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(-cameraRotationY, cameraRotationX, 0), rotationSmoothlyCoefficient);
    camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, zoom, zoomSmoothlyCoefficient);

    if (doFolowToPlayer && transform.position.x <= cameraPosition.x + 0.5 && transform.position.x > cameraPosition.x - 0.5 &&
        transform.position.y <= cameraPosition.y + 0.5 && transform.position.y > cameraPosition.y - 0.5 &&
        transform.position.z <= cameraPosition.z + 0.5 && transform.position.z > cameraPosition.z - 0.5)
    {
        cameraRotationY = -mainCamera.transform.localEulerAngles.x;
        cameraRotationX = mainCamera.transform.localEulerAngles.y;
        doRotationFolowToPlayer = true;
        doFolowToPlayer = false;
    }

    if (doRotationFolowToPlayer && transform.localEulerAngles.x <= -cameraRotationY + 0.5 && transform.localEulerAngles.x > -cameraRotationY - 0.5 &&
        transform.localEulerAngles.y <= cameraRotationX + 0.5 && transform.localEulerAngles.y > cameraRotationX - 0.5)
    {
        doRotationFolowToPlayer = false;
    }
}