- Home /
Player move and camera problem
Hi everyone, I've started to use Unity3D one week ago... I've done a lot of trials before ask this... I've change code a lot of times... I've been able to solve some errors... But i didn't understand where is this bug... It works fine at start but when you move some mouse and keyboard, it gives some problems... This JavaScript have to be applicated to a Player (Capsule) with behind a Main Camera (used this to semplify work)... So if someone can help me, A lot of thanks!!!
Code:
private var direction : Vector3; // Direction of player to know next position
function Update () {
Camera.main.transform.LookAt(transform); // Normal LookAt function
direction = Camera.main.transform.forward; // I've set the direction of player like camera's one.
transform.Translate(direction * Input.GetAxis("Vertical"),Space.World); // Up/Down move player near/further than camera
transform.Translate(direction.z * Input.GetAxis("Horizontal"),0,direction.x * -Input.GetAxis("Horizontal"),Space.World); // Horizontal Movement
var mouX : float = Input.GetAxis("Mouse X");
var mouY : float = Input.GetAxis("Mouse Y");
transform.RotateAround(transform.position,Vector3.up,mouX); // Move Player and camera horizontally by mouse
var euler = Camera.main.transform.eulerAngles.x; // See camera's rotation to put a limit
if(euler < 180){
if((euler < 50) || (mouY > 0)) // I don't want a lot of vertical vision
Camera.main.transform.RotateAround(transform.position,Vector3.right,-mouY); // To look Around
}else{
if((euler > 330) || (mouY < 0))
Camera.main.transform.RotateAround(transform.position,Vector3.right,-mouY);
}
Screen.lockCursor = true; // Normal Cursor locked function
}
@Warriorubi You say "it gives some problems" but never specify what the problems are. Can you explain what is going wrong. Is the camera parented to the player? Its most likely a conflict between your Euler limits and the LookAt function.
@hijinxbassist I didn't think about it... Yes.. The camera is parented to the player... I try to do an explication of the problem but its better see it by yourself... So, when i move the mouse up (this without keyboard input) it turns correctly and shows me the down (for the LookAt function)... but if i before turn the mouse left or right by 90° and then i try to go up with the mouse... i can't.. i hope that you can understand...
Answer by Wolfram · May 31, 2012 at 09:16 PM
Vector3.right will always rotate around the world X axis. Once you rotate your character around Y, this is no longer what you want.
So instead, rotate around the local X axis of your character:
Camera.main.transform.RotateAround(transform.position,transform.right,-mouY);
Note on the other hand, the use of Vector3.up is generally fine, since you usually still want to rotate around world Y instead of local Y, even if your character is facing up or down.
Only one word can make me feel crazy. Unbelievable. Thanks a lot!!! You have solved a two days' problem. Now my head is again free... I can think again!!!
Answer by umer772 · Aug 17, 2017 at 05:12 AM
when player moves grass on terrain removed after some distance and no grass seen on terrain after some distance pls help to sovle it????
Please post this as a new, separate question. It is unrelated to this question, and will also not be seen here (except by the two people subscribed to this question...).
Your answer
Follow this Question
Related Questions
Camera problem 2 Answers
How to make a camera that moves with the player? 1 Answer
Why this spawnpoint script locks my player? 0 Answers
Erratic movement problem 2 Answers
3rd person player 0 Answers