Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Third_Motor : MonoBehaviour {
- public Vector3 CameraDirection;
- private Vector3 MoveDirection = Vector3.zero;
- public float WalkSpeed = 10f;
- public float RunSpeed = 20f;
- public float jumpSpeed = 12;
- public bool grounded = false;
- private Rigidbody rBody;
- void Awake()
- {
- rBody = GetComponent<Rigidbody>();
- }
- // Update is called once per frame
- void Update () {
- // gets the input from the keys up.down.left.right and makes the new vector
- MoveDirection = new Vector3(Input.GetAxis("Horizontal"),0, Input.GetAxis("Vertical"));
- ///Makes the charcater move in the foward direction
- MoveDirection = transform.TransformDirection(MoveDirection);
- if(MoveDirection.magnitude > 1.0f || MoveDirection.magnitude < 0f)
- {
- MoveDirection.Normalize();
- }
- float gravity = Physics.gravity.y * 10;
- Vector3 move = rBody.velocity;
- RaycastHit hit;
- if(Physics.Raycast(transform.position, Vector3.down, out hit, 0.1f))
- {
- Debug.DrawLine(transform.position, hit.point);
- grounded = true;
- }
- else
- {
- grounded = false;
- }
- if(grounded){
- if(Input.GetKeyDown(KeyCode.Space)){
- move.y = jumpSpeed;
- grounded = false;
- }
- move = MoveDirection * WalkSpeed;
- move.y = gravity * Time.deltaTime;
- } else {
- move.x = Mathf.Clamp(move.x + MoveDirection.x * 0.01f * WalkSpeed, -WalkSpeed, WalkSpeed);
- move.y = Mathf.Clamp(move.y + gravity * Time.deltaTime, gravity, jumpSpeed);
- move.z = Mathf.Clamp(move.z + MoveDirection.z * 0.01f * WalkSpeed, -WalkSpeed, WalkSpeed);
- }
- move = MoveDirection * WalkSpeed;
- move.y = gravity * Time.deltaTime;
- transform.rotation = Third_Camera.Instance.transform.rotation;
- Vector3 euler = transform.eulerAngles;
- euler.x = 0;
- transform.eulerAngles = euler;
- rBody.velocity = move;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement