Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

335
Vistas
Unity forward position = camera angle

Hi I am very new to Unity and only got basic knowledge. 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. Now I want the camera rotation angle to be the forward 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. player.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

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. Do someone has a simple solution I can understand?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You could simply take the direction and rotate it along with the object that component is attached to using Quaternion * Vector3 operator

var rotatedDirection = transform.rotation * direction;

Or if you rather need the orientation of the other script attached to a different object then do e.g.

// 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;
over 4 years ago · Santiago Trujillo Denunciar

0

Since you're getting the rotation directly from a Transform, you could use Transform.TransformDirection:

Vector3 rotatedDirection = transform.TransformDirection(direction);
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda