Search Unity

Assistance Requested for C# Script

Discussion in 'Scripting' started by Aaron Via, May 29, 2015.

  1. Aaron Via

    Aaron Via

    Joined:
    May 29, 2015
    Posts:
    6
    I am a new person to design and am currently fooling around with making an FPS. I am stuck on a problem, however, when making it so that the camera can look up and down at a limit of 60 degrees either way.
    My script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FirstPersonController : MonoBehaviour {
    5.     public float movementSpeed = 5.0f;
    6.     public float mouseSensitivity = 3.0f;
    7.     float rotVertical = 0;
    8.     public float VerticalLimit = 60;
    9.     // Use this for initialization
    10.     void Start () {
    11.    
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.         //Rotation
    17.         float rotHorizontal = Input.GetAxis ("Mouse X")* mouseSensitivity;
    18.         transform.Rotate (0, rotHorizontal, 0);
    19.  
    20.         Camera.main.transform.localRotation = Quaternion.Euler (rotVertical, 0, 0);
    21.         rotVertical = Input.GetAxis ("Mouse Y")* mouseSensitivity;
    22.         rotVertical = Mathf.Clamp (rotVertical, -VerticalLimit, VerticalLimit);
    23.  
    24.         //Movement
    25.         float forwardSpeed = Input.GetAxis ("Vertical")* movementSpeed;
    26.         float sideSpeed = Input.GetAxis ("Horizontal")* movementSpeed;
    27.         Vector3 speed = new Vector3 (sideSpeed, 0, forwardSpeed);
    28.         speed = transform.rotation * speed;
    29.         CharacterController cc = GetComponent<CharacterController> ();
    30.         cc.SimpleMove (speed);
    31.  
    32.     }
    33. }
    34.  
    The result of this is the vertical movement seems to be locked a zero, because it attempts to move upwards and downwards but immediately and repeatedly gets snapped back. Any assistance would be fantastic.
    Thanks!
     
    Last edited: May 29, 2015
  2. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    Please put your code in between the CODE tags...
     
  3. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    You should join the camera and the head/arms of the player, so you will only have to move 1 transform to move all objects.
     
  4. Aaron Via

    Aaron Via

    Joined:
    May 29, 2015
    Posts:
    6
    Just did - sorry. This is my first post on the forums.
     
  5. Aaron Via

    Aaron Via

    Joined:
    May 29, 2015
    Posts:
    6
    I can see how that would work but right now the character is just an empty entity and I'm trying to just latch the camera's vertical movement to the mouse's Y-axis.
     
  6. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    I haven't checked the code for a bug, but the first thing I'd check is the VerticalLimit value in the inspector to make sure it's not 0. That will override this line you have here:

    Code (csharp):
    1. public float VerticalLimit =60;
    So I'd make sure it's 60 or whatever in the inspector first if you haven't already.
     
  7. Aaron Via

    Aaron Via

    Joined:
    May 29, 2015
    Posts:
    6
    Thanks for your help guys, but I just figured it out. It was a small change by adding a "-" to the line:
    Code (CSharp):
    1. rotVertical -= Input.GetAxis ("Mouse Y")* mouseSensitivity;