- Home /
Moving the camera with mouse not working
Basically I'm very new to C# and despite being able to move my player (cube) forward,back,left and right, I can't work out why I can't rotate the camera with my mouse which is attached to the cube.
This is what i've tried plus many other combinations of code and all have brought compiler errors:
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour {
 public float playerSpeed = 20.0f;
 public float playerSpeedSideways = 5.0f;
 public float rotationSpeed = 100.0f;
 // Use this for initialization
 void Start () {
     transform.position = new Vector3 (0,0,0);
 
 
 }
 
 // Update is called once per frame
 void Update () {
     transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * Time.deltaTime * playerSpeedSideways);
     transform.Translate(Vector3.back * Input.GetAxis("Backwards") * Time.deltaTime * playerSpeedSideways);
     transform.Translate(Vector3.forward * Input.GetAxis("Forwards") * Time.deltaTime * playerSpeed);
     transform.Rotate(Vector3.MoveTowards * Input.GetAxis("Mouse X"));
     transform.Rotate(Vector3.MoveTowards * Input.GetAxis("Mouse X"));
}
}
Any help, explaining why the code isn't working would be great. Thanks in advance
Ali g
Answer by RyanZimmerman87 · Jan 22, 2014 at 12:16 AM
Here's a pretty basic movement + mouselook script. I can't take credit for the code but this helped me a lot when I started my first FPS (sorry for formatting a little messed up:
 using UnityEngine;
 using System.Collections;
 
 [AddComponentMenu("Camera-Control/Mouse Look")]
 public class MouseLookScript : MonoBehaviour 
 {
     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
     public RotationAxes axes = RotationAxes.MouseXAndY;
     
     public float sensitivityX = 10F;
     public float sensitivityY = 10F;
     
     public float minimumX = -360F;
     public float maximumX = 360F;
     
     public float minimumY = -60F;
     public float maximumY = 60F;
     
     float rotationY = 0F;
 
     public float cameraSpeedFloat;
 
     Transform cameraTransform;
 
 void Start ()
     {
 
         cameraTransform = transform;
 
 if (rigidbody)
         {
             rigidbody.freezeRotation = true;
         }
 }
 
 void Update()
     {
 
         //Look direction
         if (axes == RotationAxes.MouseXAndY) 
         {
             float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
             
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
             
             cameraTransform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
         }
 
         else if (axes == RotationAxes.MouseX)
         {
             cameraTransform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
         }
 
         else
         {
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
             
             cameraTransform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
         }
 
 
         //Movement direction
         if (Input.GetKey (KeyCode.W)) 
         {
             cameraTransform.position += cameraTransform.forward * cameraSpeedFloat * Time.deltaTime;
         }
 
          if (Input.GetKey (KeyCode.S)) 
         {
             cameraTransform.position += -cameraTransform.forward * cameraSpeedFloat * Time.deltaTime;
         }
 
         if (Input.GetKey (KeyCode.A)) 
         {
             cameraTransform.position += -cameraTransform.right * cameraSpeedFloat * Time.deltaTime;
         }
 
         if (Input.GetKey (KeyCode.D)) 
         {
             cameraTransform.position += cameraTransform.right * cameraSpeedFloat * Time.deltaTime;
         }
 }
 
 
 }
Make sure to adjust the variables to what you want like the cameraSpeedFloat, sensitivity X and Y, and mimimum maximum Y.
Thanks RyanZimmerman87! Added your code to the camera and it rotated by the mouse move!
I used it to. And works fine... thanks, thanks, thanks Ryan
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
beginner programming woe grids 1 Answer
[Answered]Rigid body Movement C# 0 Answers
Help with Basic Movement Script 1 Answer
Get general direction of mouse/tap location relative to player on mouse click? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                