Jump to content
Unity Insider Forum

Relative Bewegung zur Kamera (CubeWorld)


Garzec

Recommended Posts

Hallo,

ich versuche eine Steuerung, wie man sie in CubeWorld wiederfindet. Ich kann den Spieler über die Inputs bewegen und die Kamera um den Spieler rotieren. Mein einziges Problem dazu ist grade noch die Spieler Rotation.

 

public class CameraMovement : MonoBehaviour
{
    private float currentRotationX;
    private float currentRotationY;
    private Transform playerTransform;
 
    private const float FOLLOW_DISTANCE = 10;
    private const float VERTICAL_ROTATION_SPEED = 5;
    private const float HORIZONTAL_ROTATION_SPEED = 10;
    private const float VERTICAL_ROTATION_MIN = 10;
    private const float VERTICAL_ROTATION_MAX = 80;
 
    private static CameraMovement instance;
 
    public static CameraMovement Instance { get { return instance; } }
 
    private void Awake()
    {
        instance = this;
    }
 
    private void Start()
    {
        playerTransform = PlayerMovement.Instance.transform;
    }
 
    private void Update()
    {
        SetInputs();
    }
 
    private void LateUpdate()
    {
        FollowTarget();
        LookAtTarget();
    }
 
    private void FollowTarget()
    {
        transform.position = GetFollowPosition();
    }
 
    private Vector3 GetFollowPosition()
    {
        Vector3 direction = new Vector3(0, 0, -FOLLOW_DISTANCE);
        Quaternion cameraRotation = Quaternion.Euler(currentRotationY, currentRotationX, 0);
        return playerTransform.position + cameraRotation * direction;
    }
 
    private void LookAtTarget()
    {
        transform.LookAt(playerTransform);
    }
 
    private void SetInputs()
    {
        currentRotationX += Input.GetAxis("Mouse X");
        currentRotationY += Input.GetAxis("Mouse Y");
 
        LimitVerticalRotation();
    }
 
    private void LimitVerticalRotation()
    {
        currentRotationY = Mathf.Clamp(currentRotationY, VERTICAL_ROTATION_MIN, VERTICAL_ROTATION_MAX);
    }
}
public class PlayerMovement : MonoBehaviour
{
    private Rigidbody rigid;
    private float horizontalInput;
    private float verticalInput;
    private bool jumpPressed;
 
    private static PlayerMovement instance;
 
    public static PlayerMovement Instance { get { return instance; } }
 
    private void Awake()
    {
        instance = this;
    }
 
    private void Start()
    {
        rigid = GetComponent<Rigidbody>();
    }
 
    private void Update()
    {
        SetMovementInputs();
    }
 
    private void FixedUpdate()
    {
        Move();
        Rotate();
    }
 
    private void Move()
    {
        rigid.velocity = GetMovement();
    }
 
    private void Rotate()
    {
        // transform.rotation = GetRotation();
    }
 
    private Vector3 GetMovement()
    {
        Vector2 movementDirection = GetMovementDirection();
        float verticalMovement = rigid.velocity.y;
        Vector3 movement = new Vector3(movementDirection.x, verticalMovement, movementDirection.y);
 
        return movement * PlayerProfile.Instance.MovementSpeed;
    }
 
    //private Quaternion GetRotation()
    //{
    //    Vector2 movementDirection = GetMovementDirection();
    //    Vector3 rotationDirection = CameraMovement.Instance.transform.TransformDirection(movementDirection);
    //    rotationDirection.y = 0;
 
    //    Vector3 targetRotation = rotationDirection.normalized * movementDirection.magnitude;
    //    targetRotation *= PlayerProfile.Instance.RotationSpeed;
 
    //    return Quaternion.LookRotation(targetRotation, Vector3.up);
    //}
 
    private Vector2 GetMovementDirection()
    {
        return new Vector2(horizontalInput, verticalInput);
    }
 
    private void SetMovementInputs()
    {
        horizontalInput = Input.GetAxis("Horizontal");
        verticalInput = Input.GetAxis("Vertical");
    }
}

Ich habe die Methode GetRotation mal auskommentiert. Benutze ich diesen Code, behält der Spieler seine Rotation nicht bei, sondern rotiert nur zur Seite, wenn ich den Button gedrückt halte (der Input 1 ist).

Könnte mir jemand dabei helfen? :)

Link zu diesem Kommentar
Auf anderen Seiten teilen

private Quaternion GetRotation() {
	Vector2 movementDirection = GetMovementDirection();
	// Achtung. Ich weiß nicht, wie diese Konvertierung implementiert ist. Vermutlich wird z auf 0 gesetzt, was falsch wäre.
	//Vector3 worldDir = movementDirection;
	// Besser:
	Vector3 worldVec = new Vector3(movementDirection.x, 0, movementDirection.y);
	// Noch besser: Du verwendest Vector2 nicht in einem 3D-Spiel
	
	Vector3 euler = Camera.main.transform.rotation.eulerAngles;
	euler.x = 0;
	// Y Bleibt über
	euler.z = 0;
	
	Vector3 camRelVec = Quaternion.Euler(euler) * worldVec;
	return Quaternion.LookRotation(camRelVec, Vector3.up);
}

 

Link zu diesem Kommentar
Auf anderen Seiten teilen

Der Spieler kann zwar rotieren, aber nicht "schräg" gucken, heißt er rotiert nur nach vorne, links, rechts oder nach hinten. Steht er diagonal, rotiert er von selbst wieder "gerade".

    private Quaternion GetRotation(Vector2 movementDirection)
    {
        Vector3 worldDirection = new Vector3(movementDirection.x, 0, movementDirection.y);

        Vector3 eulerRotation = CameraMovement.Instance.transform.rotation.eulerAngles;
        eulerRotation.x = 0;
        eulerRotation.z = 0;

        Vector3 relativeRotation = Quaternion.Euler(eulerRotation) * worldDirection;
        return Quaternion.LookRotation(relativeRotation, Vector3.up);
    }

 

Link zu diesem Kommentar
Auf anderen Seiten teilen

Archiviert

Dieses Thema ist jetzt archiviert und für weitere Antworten gesperrt.

×
×
  • Neu erstellen...