*So the problem is this, I can’t rotate my character when I turn my camera. When I rotate 180º I press W and my Character go backwards (same for the other keys. ) *
PlayerMovement Script
public GameObject Camera01;
//Variables for Player Movement
public float speedWalk = 2.0f; //Velocidade de Andar
public float speedRun = 12.0f; //Velocidade de Correr
public float speedCrch = 2.0f; //Velocidade de Agachado
public Rigidbody rb;
private float mass;
void Start()
{
Camera01 = GetComponent<GameObject>();
rb = GetComponent<Rigidbody>();
rb.mass = mass;
}
void FixedUpdate()
{
//Char > Movement
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveVertical, 0.0f, moveHorizontal);
rb.velocity = movement * speedWalk;
rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
//Mouse look > Script
transform.rotation = Quaternion.Euler(0, Camera01.GetComponent<MouseLook>().currentYRotation, Camera01.GetComponent<MouseLook>().currentYRotation);
}
public int tilt { get; set; }
MouseLook Script
public GameObject Camera01;
public float lookSensitivity = 5f;
public float xRotation;
public float yRotation;
public float currentXRotation;
public float currentYRotation;
public float xRotationV;
public float yRotationV;
public float lookSmoothDamp = 0.1f;
//Init
void Start()
{
}
//FixedUpdate
void FixedUpdate()
{
xRotation -= Input.GetAxis ("Mouse Y") * lookSensitivity;
yRotation += Input.GetAxis ("Mouse X") * lookSensitivity;
xRotation = Mathf.Clamp(xRotation, -90, 90);
currentXRotation = Mathf.SmoothDamp (currentXRotation, xRotation, ref xRotationV, lookSmoothDamp);
currentYRotation = Mathf.SmoothDamp (currentYRotation, yRotation, ref yRotationV, lookSmoothDamp);
transform.rotation = Quaternion.Euler(0, currentYRotation, currentXRotation);
}
I edited your scripts in order to make them work. I included a lot of comments to show you what I changed. Mainly, in your original scripts you had some axes the wrong way round, and also I make it work without a rigidbody. I don’t know why you want to use these scripts (even my version that work). It would be better if you used the “First Person Character Controller” from Unity’s “Standard Assets”. Anyway, here are the corrected scripts:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public GameObject Camera01; //Camera01 MUST be dragged and dropped in this Inspector field
//Variables for Player Movement
public float speedWalk = 2.0f; //Velocidade de Andar
public float speedRun = 12.0f; //Velocidade de Correr
public float speedCrch = 2.0f; //Velocidade de Agachado
private Rigidbody rb;
private float mass;
public int tilt { get; set; }
private MouseLook mouseLook; //variable to cached the MouseLook script for better performance
void Start()
{
//Camera01 = GetComponent<GameObject>();
rb = GetComponent<Rigidbody>(); //Rigidbody not used in this version of the script, so if it's not needed it can be deleted
//rb.mass = mass; // assignement is made the wrong way around => rb.mass should be assigned to mass (see below
mass = rb.mass; //however mass is not (yet at least)used anywhere in this script, so if it's not needed it can be deleted
//cache MouseLook script
mouseLook = Camera01.GetComponent<MouseLook>();
}
void FixedUpdate()
{
//Char > Movement
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
//Vector3 movement = new Vector3(moveHorizontal, 0.0f , moveVertical);
//rb.velocity = movement * speedWalk;
//rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt);
//Rotate Left/Right towards the camera rotation, in order to always face forwards
//Tilting the player's "body" Up/Down where "his eyes"are looking is not really necessary
//So, rotating around the x-axis is not really necessary
//NOTE: In the original script the player was rotating by mistake around the z-axis
transform.rotation = Quaternion.Euler(0f, mouseLook.currentYRotation, 0f);
transform.position += (transform.forward * moveVertical * speedWalk) + (transform.right * moveHorizontal * speedWalk);
}
}
using UnityEngine;
using System.Collections;
public class MouseLook : MonoBehaviour {
//public GameObject Camera01; //not needed can be deleted
public float lookSensitivity = 5f;
public float xRotation;
public float yRotation;
public float currentXRotation;
public float currentYRotation;
public float xRotationV;
public float yRotationV;
public float lookSmoothDamp = 0.1f;
//Init
void Start()
{
}
//FixedUpdate
void FixedUpdate()
{
xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity; //controls up/down look => mouse forward/backwards corresponds to camera rotation about x-axis
yRotation += Input.GetAxis("Mouse X") * lookSensitivity; //controls left/right look => mouse left/right corresponds to camera rotation about y-axis
xRotation = Mathf.Clamp(xRotation, -90, 90);
currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, ref xRotationV, lookSmoothDamp);
currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, ref yRotationV, lookSmoothDamp);
//rotates camera - player should rotate with camera rotation ONLY left/right
//i.e. camera up/down should correspond only to player eye movement, not player body movement
//So, PlayerMovement script should check camera's rotation in order to rotate player accordingly
//So, PlayerMovement script MUST have a reference to the Camera GameObject in order to know its rotation
transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
}
}