Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerController : MonoBehaviour
- {
- //Variables
- public float speed = 6.0F;
- public float jumpSpeed = 8.0F;
- public float gravity = 20.0F;
- private Vector3 moveDirection = Vector3.zero;
- void Update()
- {
- CharacterController controller = GetComponent<CharacterController>();
- // is the controller on the ground?
- if (controller.isGrounded)
- {
- //Feed moveDirection with input.
- moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
- moveDirection = transform.TransformDirection(moveDirection);
- //Multiply it by speed.
- moveDirection *= speed;
- //Jumping
- if (Input.GetButton("Jump"))
- moveDirection.y = jumpSpeed;
- if (transform.position.y < -7)
- {
- transform.position = new Vector3(0, 2.7f, 0);
- }
- }
- //Applying gravity to the controller
- moveDirection.y -= gravity * Time.deltaTime;
- //Making the character move
- controller.Move(moveDirection * Time.deltaTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement