Advertisement
CarlosWGama

PlayerController 3D

Sep 9th, 2022
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour {
  6.     public float speed = 25.0F;
  7.      public float jumpSpeed = 8.0F;
  8.      public float gravity = 20.0F;
  9.      private Vector3 moveDirection = Vector3.zero;
  10.      private float turner;
  11.      private float looker;
  12.      public float sensitivity = 5;
  13.      
  14.      // Use this for initialization
  15.      void Start () {
  16.          
  17.      }
  18.      
  19.      // Update is called once per frame
  20.      void Update () {
  21.          CharacterController controller = GetComponent<CharacterController>();
  22.          // is the controller on the ground?
  23.          if (controller.isGrounded) {
  24.              //Feed moveDirection with input.
  25.              moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  26.              moveDirection = transform.TransformDirection(moveDirection);
  27.              //Multiply it by speed.
  28.              moveDirection *= speed;
  29.              //Jumping
  30.              if (Input.GetButton("Jump"))
  31.                  moveDirection.y = jumpSpeed;
  32.              
  33.          }
  34.          turner = Input.GetAxis ("Mouse X")* sensitivity;
  35.          looker = -Input.GetAxis ("Mouse Y")* sensitivity;
  36.          if(turner != 0){
  37.              //Code for action on mouse moving right
  38.              transform.eulerAngles += new Vector3 (0,turner,0);
  39.          }
  40.          if(looker != 0){
  41.              //Code for action on mouse moving right
  42.              transform.eulerAngles += new Vector3 (looker,0,0);
  43.          }
  44.          //Applying gravity to the controller
  45.          moveDirection.y -= gravity * Time.deltaTime;
  46.          //Making the character move
  47.          controller.Move(moveDirection * Time.deltaTime);
  48.      }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement