The question is answered, right answer was accepted ! Please check not only the answered given , but read the comments bellow of the answer for more information about the situation!
My camera rotate but my player dont. How Ive to do so?
Hello C sharp and Unity Dev's,
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);
}
More print screens: ( @pako )
Player Inspector
Camera Inspector
Hierarchy
Please clarify if Player$$anonymous$$ovement Script is attached to a Player GameObject and $$anonymous$$ouseLook Script is attached on Camera GameObject. Also it would be even more helpful if you edit your question and include screenshots of the Hierarchy and Inspector, showing the Player and Camera GameObjects.
Answer by pako · Sep 13, 2015 at 04:08 PM
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);
}
}
Woaw thanks. $$anonymous$$y point on making my own script is for learning proposes ( C sharp ) :p ! But I can still use with rigid body, right? Because I want this "Player Object " interact " with the world around! And my axes are twisted because of something I did on Input (x , but now I reset them and should work with this re-new script.
I really appreciate your help , I'll put them now and see what happens, then I'll update you here or by P$$anonymous$$. And sorry for my English. @pako
@$$anonymous$$rVersus you are welcome! I suspected you did it for learning purposes, this is why I included so many comments, so you understand the changes I made.
Of course you can have a Rigidbody on the player for other interactions with game world. I tried very hard to make the movement work properly using the rigidbody.velocity for the movement, as was the original script, but I didn't succeed. $$anonymous$$aybe it would work if I made it more complicated, but I preferred to simplify it, ins$$anonymous$$d of making it more complicated.
I tested it and it works fine. Some details:
$$anonymous$$ouseLook is not need on Player$$anonymous$$ovement GameObject. I have deleted it in my test project.
$$anonymous$$ouseLook script only on Camera01 GameObject
In Player$$anonymous$$ovement script, you must drag-n-drop the Camera01 GameObject inside the "Camera 01" field in the Inspector, otherwise player rotation will not work