- Home /
Rotate charcter with camera?
How do i make a script something like the one below but that will make the character rotate with the camera on x axis so it's like a first-person shooter.
var speed = 6.0; var jumpSpeed = 8.0; var gravity = 20.0; var camera : GameObject;
private var moveDirection = Vector3.zero; private var grounded : boolean = false;
function FixedUpdate() { if (grounded) { // We are grounded, so recalculate movedirection directly from axes moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection = speed; transform.Rotate(Vector3.right camera);
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
Please try to explain better what you are trying to do. If you just want a camera that follows closely behind or with your player, you just need to make the camera a child of the player, no script required.
i want it to be first person so i need the charcter to rotate when the camera rotates sideways so pressing w would move you foward even if it was origanly sideays or something do you understand... like call of duty
Answer by superpig · Apr 22, 2011 at 01:47 AM
It's better to do it as a separate component, instead of modifying the movement code. The MouseLook component should do it.
Your answer
Follow this Question
Related Questions
Make FirstPersonCam follow car direction 1 Answer
How can i make change the First Person position by clicking a Buttom? 3 Answers
First Person Controller gravity changing script. 2 Answers
Look to the side-Scripting Problem 2 Answers
I am getting a problem with moving and rotating a camera! 1 Answer