- Home /
Movement with Camera
Hi everyone, I have a problem with my movement or camera or maybe both. I made two seperate script which are PlayerMovement which has the players movement in C# and a MouseLook which enables you to look around in 360 degrees also in C#. Now everything works expect when I move forward and lets say I turn my camera to the right the player starts to strafe right and if I turn the camera to the right again he is now moving backwards. By the way this is a Third Person Game. Any Ideas on how I can fix this?
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float speed = 5.0f;
public float jumpSpeed = 5.0f;
public float gravity = 20.0f;
public float run = 2.0f;
public float crouchSpeed = 0.5f;
private Vector3 moveDirection = Vector3.zero;
void Start()
{
transform.position = new Vector3(0,1.7f,0);
}
void Update()
{
CharacterController controller = GetComponent<CharacterController> ();
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
moveDirection = transform.TransformDirection (moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump"))
moveDirection.y = jumpSpeed;
if (Input.GetKey(KeyCode.LeftShift))
moveDirection *= run;
if (Input.GetButton("Crouch"))
//transform.position.y -= 1.5;
//transform.localScale.y -= new Vector3(0,.5f,0);
moveDirection *= crouchSpeed ;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move (moveDirection * Time.deltaTime);
}
}
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationX = 0F;
float rotationY = 0F;
Quaternion originalRotation;
void Update ()
{
if (axes == RotationAxes.MouseXAndY)
{
// Read the mouse input axis
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
rotationY = ClampAngle (rotationY, minimumY, maximumY);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
else if (axes == RotationAxes.MouseX)
{
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = ClampAngle (rotationY, minimumY, maximumY);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * yQuaternion;
}
}
void Start ()
{
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
originalRotation = transform.localRotation;
}
public static float ClampAngle (float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp (angle, min, max);
}
}
Answer by Hachley · May 11, 2014 at 01:46 PM
What you need to do is that if you move to the right(with your mouse) then also rotate the face of your players body to the right. What you have now is that if you hold W and look straight with your mouse, then the players body moves forward, but once you turn your camera 90 degrees to the right, you only rotate the camera but not the whole players body. If you would to rotate the body at the same direction where you rotate the camera, then the "front" of the body would always be where you are facing with your camera.
If you look at the default first-person-controller that unity provides, then go inside the mouse-look script and find the bit that tells you that it rotates the body every time you move the camera. They //comment every section of the script.
Answer by GhostDre · May 11, 2014 at 05:18 PM
Where would I find the FPS Controller. And if so can I see an example?
I suggest you see some tutorials and lessons that Unity offers here, otherwise you would know about the FPS Controller. Anyway, you can right-click on your project window, then click "Import Package">"Character Controller", after that you can find the character controller in the folders and then drag it into the scene, you will see a capsule player object in the game. Also, if you run the game now, then you will be able to play with the imported character right away(unless you have another in the scene). If you go into inspector of the character controller, you will see many scripts attached to it. Once you click on the "$$anonymous$$ouseLook" script, inside you will find the code corresponding to the mouse look movement that also only rotates the characters body right/left(according to your mouse) but not up/down, so that your character doesn't tilt his body.
Check it out yourself, there's a lot of useful stuff there, and I'd suggest you start with that character controller and just modify it to your needs.
Your answer
Follow this Question
Related Questions
How to make my player walk according to the cameras direction? 2 Answers
Third Person Camera Help? 0 Answers
Constraining a camera movement 0 Answers
How to make my player look in the direction of its movement? 3 Answers
Moving (teleporting) player and camera when they enter a certain area 5 Answers