Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

FPS controller

Discussion in 'Scripting' started by ShadowToast, Feb 6, 2016.

  1. ShadowToast

    ShadowToast

    Joined:
    May 7, 2015
    Posts:
    8
    Hello. I've made my own character controller that can walk around, can jump, has gravity and can turn left and right. I've set camera as a child of the controller. This is my code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour {
    5.     public float speed = 10.0F;
    6.     public float jumpSpeed = 8.0F;
    7.     public float gravity = 20.0F;
    8.     public float sensitivity = 8.0F;
    9.     public float turnSpeed = 5.0F;
    10.  
    11.     private float moveX;
    12.     private float newmoveX;
    13.     private float DmoveX;
    14.     private float moveXv;
    15.  
    16.     private Vector3 moveDirection = Vector3.zero;
    17.  
    18.     void Update()
    19.     {
    20.         //Move and Jump
    21.         CharacterController controller = GetComponent<CharacterController>();
    22.         if (controller.isGrounded)
    23.         {
    24.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    25.             moveDirection = transform.TransformDirection(moveDirection);
    26.             moveDirection *= speed;
    27.             if (Input.GetButton("Jump"))
    28.                 moveDirection.y = jumpSpeed;
    29.  
    30.         }
    31.         moveDirection.y -= gravity * Time.deltaTime;
    32.         controller.Move(moveDirection * Time.deltaTime);
    33.  
    34.         //Look Left and Right
    35.         moveX = Input.GetAxis("Mouse X") * sensitivity;
    36.         newmoveX = Mathf.SmoothDamp(moveX, DmoveX, ref moveXv, turnSpeed);
    37.         transform.Rotate(0, newmoveX, 0);
    38.     }
    39. }
    My problem is I want to look up and down. I don't know how can I reference the camera (that is the child) and make it rotate up and down.
     
  2. ShadowToast

    ShadowToast

    Joined:
    May 7, 2015
    Posts:
    8