简体   繁体   English

unity forward position = 摄像机角度

[英]Unity forward position = camera angle

Hi I am very new to Unity and only got basic knowledge.嗨,我对 Unity 很陌生,只了解基本知识。 I created a player who can move and also a camera which follows the player and according to the mouseX and mouseY acis the camera rotates.我创建了一个可以移动的玩家和一个跟随玩家的相机,并根据 mouseX 和 mouseY acis 相机旋转。 Now I want the camera rotation angle to be the forward position.现在我希望相机旋转角度为前向 position。

If the player press w he moves along x acis but if the camera angle change and the player press w it should move into this direction.如果玩家按下 w,他会沿着 x acis 移动,但如果摄像机角度发生变化并且玩家按下 w,它应该会朝这个方向移动。 player.cs播放器.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DoublePlayer : MonoBehaviour
{
    private CharacterController _controller;
    public float _jumpHeight = 3f;
    [SerializeField]
    private float _moveSpeed = 5f;
    [SerializeField]
    private float _gravity = 9.81f;
    private float _directionY;
    void Start()
    {
        _controller = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");
        Vector3 direction = new Vector3(horizontalInput, 0, verticalInput);
        
        // set gravity
        _directionY -= _gravity * Time.deltaTime;
        direction.y = _directionY;

        _controller.Move(direction * _moveSpeed * Time.deltaTime);
    }
}

camera.cs相机.cs

public class CameraController : MonoBehaviour
{
    public float RotationSpeed = 1;
    public Transform Target, Player;
    float mouseX, mouseY;
    void Start()
    {
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void LateUpdate()
    {
        CamControl();
    }
    void CamControl()
    {
        mouseX += Input.GetAxis("Mouse X") * RotationSpeed;
        mouseY += Input.GetAxis("Mouse Y") * RotationSpeed;
        mouseY = Mathf.Clamp(mouseY, -35, 60);

        transform.LookAt(Target);

        Target.rotation = Quaternion.Euler(mouseY, mouseX, 0);
        Player.rotation = Quaternion.Euler(0, mouseX, 0);
    }
}

For Target and Player variable I selected the player object.对于目标和播放器变量,我选择播放器 object。 Do someone has a simple solution I can understand?有人有我能理解的简单解决方案吗?

You could simply take the direction and rotate it along with the object that component is attached to using Quaternion * Vector3 operator您可以使用Quaternion * Vector3运算符简单地确定direction并将其与组件附加到的 object 一起旋转

var rotatedDirection = transform.rotation * direction;

Or if you rather need the orientation of the other script attached to a different object then do eg或者,如果您更需要附加到不同 object 的其他脚本的方向,请执行例如

// link your camera or whatever shall be used for the orientation via the Inspector
[SerializeField] private Transform directionProvider;

and then接着

var rotatedDirection = directionProvider.rotation * direction;

Since you're getting the rotation directly from a Transform , you could use Transform.TransformDirection :由于您直接从Transform获得旋转,您可以使用Transform.TransformDirection

Vector3 rotatedDirection = transform.TransformDirection(direction);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM