Question by
TylerStudios · Apr 15, 2016 at 01:04 AM ·
c#cameramovement
Player Movement with Camera Script
The player wont move (WASD) but the camera works
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse Look")]
public class playerController : 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;
public int speed;
float rotationY = 0F;
public float cameraSpeedFloat;
Transform cameraTransform;
void Start()
{
cameraTransform = transform;
if (GetComponent<Rigidbody>())
{
GetComponent<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;
}
}
}
Comment
Best Answer
Answer by Kurdle_4855 · Apr 15, 2016 at 03:28 AM
Your 'cameraSpeedFloat' could be zero, OR it is in fact moving, just a VEERY small amount. This can be fixed by multiplying the delta time or setting the camerSpeed to a much higher value.