I tried to add some movements and having the camera follow the player. At first it looked like it worked, but then i realized that the directions are set to the buttons. If i turn around, it feels like everything is messed up. Left is right etc. I guessthe problem is the player is moved on the axis and that no part of the script senses which direction the player character is facing. How to solve it?
Script for controlling the camera with the mouse:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollowMouse : MonoBehaviour
{
public float speedH = 2.0f;
public float speedV = 2.0f;
public float yaw = 0.0f;
public float pitch = 0.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
yaw += speedH * Input.GetAxis("Mouse X");
pitch -= speedV * Input.GetAxis("Mouse Y");
transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
}
}
Script for controlling the player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement3D : MonoBehaviour
{
private Rigidbody rb;
// Moves the object forward at two units per second.
public float speed = 2;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(x, 0, z);
movement = Vector3.ClampMagnitude(movement, 1);
transform.Translate(movement * speed * Time.deltaTime);
}
}