- Home /
Look rotation is "Snappy"
adding this script to a camera
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public int Sensitivity;
public float MaxDistance;
private float MouseY;
private float MouseYSet;
private float CameraAngleX;
private float CameraY;
private float CameraZ;
void Start () {
}
void Update () {
// Look controls
MouseY -= Input.GetAxisRaw ("Mouse Y");
CameraY = Mathf.Clamp (CameraY + (MouseY - MouseYSet) * Sensitivity * 10, transform.parent.lossyScale.y / 2, transform.parent.lossyScale.y / 2 + MaxDistance);
CameraZ = Mathf.Clamp (CameraZ + (MouseY - MouseYSet) * Sensitivity * 10, -MaxDistance, -MaxDistance / 4 * 3);
if (MouseYSet != MouseY)
{
transform.localPosition = new Vector3 (0, CameraY, CameraZ);
transform.LookAt (transform.parent.position);
MouseYSet = MouseY;
}
}
}
and this to a player
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public int Speed;
public int RunSpeed;
public int Sensitivity;
public int JumpHeight;
public int Force;
private float TerrainHeight;
private float TerrainHeightSet;
private float PlayerFoot;
private float MouseX;
private float MouseXSet;
private bool Jumping;
private bool Running;
void Start () {
Jumping = false;
Running = false;
}
void Update () {
PlayerFoot = transform.position.y - transform.lossyScale.y / 2;
TerrainHeight = Terrain.activeTerrain.SampleHeight (transform.position);
// Translation controls
if (Input.GetKey (KeyCode.W) && Running == false)
{
transform.position += transform.forward * Speed * 20 * Time.deltaTime;
}
if (Input.GetKey (KeyCode.S) && Running == false)
{
transform.position -= transform.forward * Speed * 20 * Time.deltaTime;
}
if (Input.GetKey (KeyCode.D) && Running == false)
{
transform.position += transform.right * Speed * 20 * Time.deltaTime;
}
if (Input.GetKey (KeyCode.A) && Running == false)
{
transform.position -= transform.right * Speed * 20 * Time.deltaTime;
}
// Run controls
if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.Q))
{
Running = true;
}
if (Input.GetKey (KeyCode.W) && Running == true)
{
transform.position += transform.forward * RunSpeed * 20 * Time.deltaTime;
}
if (Input.GetKeyUp (KeyCode.Q) && Input.GetKeyUp (KeyCode.W))
{
Running = false;
}
// Rotation controls
MouseX += Input.GetAxisRaw ("Mouse X");
if (MouseXSet != MouseX)
{
transform.Rotate (new Vector3 (0,(MouseX - MouseXSet) * Sensitivity * 10, 0));
MouseXSet = MouseX;
}
// Jump controls
if (Input.GetKey (KeyCode.Space) && PlayerFoot < TerrainHeight + 1 && PlayerFoot < TerrainHeight + JumpHeight * 10)
{
TerrainHeightSet = TerrainHeight;
StartCoroutine (JumpDelay ());
}
if (Jumping == true)
{
rigidbody.velocity = new Vector3 (0, (Force * 10), 0);
}
if (Input.GetKeyUp (KeyCode.Space))
{
Jumping = false;
}
if (PlayerFoot >= TerrainHeightSet + JumpHeight * 10)
{
Jumping = false;
}
if (Jumping == false)
{
rigidbody.velocity = new Vector3 (0, -(Force * 20), 0);
}
// Keep player upright
if (PlayerFoot < TerrainHeight + 1)
{
transform.position += Vector3.up * (TerrainHeight - PlayerFoot);
}
}
// Check if player really wants to jump
IEnumerator JumpDelay () {
yield return new WaitForSeconds (0.05f);
if (Input.GetKey (KeyCode.Space))
{
Jumping = true;
}
StopCoroutine (JumpDelay ());
}
}
it works fine with the looking left and right (not snappy) but vertical camera rotation is strange. it has about 20 different positions for some reason, almost like the positions are ints, but theyre not. this isnt a HUGE issue, but id like to eventually get it fixed. ive already had a look but theres nothing i can spot right now. if anyone could have a look, id appreciate it. sensitivity i set to 9 on both the camera and player, and i set maxdistance to 50.
Considered using the First Person Controller that comes with unity?
You wasted your time. the first person controller with unity is customizable and simpler. That is so much code.
$$anonymous$$other hugger who the heck is gonna read through all that? You gotta show exactly where you think the issue is or people like me aren't even gonna read it.
First off, props. Just because a script exists does not mean you have to use it.
To the people who commented saying there was too much code, incorrect. His problem clearly lies in the first script he posted. The second script is extra, a nice to have. Not to mention, reading the comments (again, props) will prune out the code you're not interested in.
Now...your problem, why aren't you just rotating your camera around the x-axis? I'm a bit confused as to why you're moving the transform and not rotating in the camera script, while you do in the player script.
@HYPSERSAVV
Hey man, you're right. $$anonymous$$aking your own script is an accomplishment. But in the real world, you want to take what's already available to you and modify it to your needs. Otherwise, I will just go write my own program$$anonymous$$g language than just using what's already available.
Let's be real, OP wasted lots of time on something that's out there and free to use by default. Just my two bits.
Answer by awplays49 · Jan 05, 2015 at 12:49 PM
It's ok guys. I already deleted the script from my game. You were all right. It was too much code. Thanks for the help.
Your answer
Follow this Question
Related Questions
Mouse look changes direction after going over center of screen 0 Answers
Mouse look Y axis sensitivity doesn't change 0 Answers
xRotation is not working,xRotation is not working why? 1 Answer
Camera movement independent of player control 1 Answer
How to convert the MouseLook(Script) to a java script. 1 Answer