使用操纵杆使角色从摄像机轴移出[Unity]

时间:2018-07-11 23:06:45

标签: c# unity3d gamepad

我最近开始使用Unity3D进行练习,并创建了一种摄像头,该摄像头使Lerp缓慢地朝着播放器的方向向玩家后面移动。我的意图是让玩家的移动基于相机所面对的方向,而不是角色的移动完全基于世界空间。

如果有人可以帮助我解决如何使角色相对于摄影机轴(而不是世界轴)移动的话,将不胜感激。

字符移动脚本

 if (gameObject.GetComponent<CharacterJumpScript>().isGrounded)
        {
            Vector3 posi = new Vector3((Input.GetAxis("LeftJoystickHorizontal") * characterMovement), 0.0f, (Input.GetAxis("LeftJoystickVertical") * characterMovement));
            this.transform.position += posi;
        }

相机跟随脚本

 private void FixedUpdate()
    {
        float rot = followTarget.gameObject.transform.rotation.eulerAngles.y;
        print(rot);
        Vector3 oldOffset = cameraOffset;
        cameraOffset = Vector3.Lerp(oldOffset, new Vector3(-Mathf.Sin(Mathf.Deg2Rad * rot) * 20.0f, 8.0f, -Mathf.Cos(Mathf.Deg2Rad * rot) * 20.0f), Time.deltaTime);
        Vector3 desiredPosition = followTarget.position + cameraOffset;
        Vector3 smoothPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
        transform.position = smoothPosition ;
        transform.LookAt(followTarget);
    }

非常感谢您。

0 个答案:

没有答案
相关问题