Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7.     public Camera camera;
  8.  
  9.     [SerializeField]
  10.     private Transform follow;
  11.     private Vector3 targetPosition;
  12.     public float currentSpeed;
  13.     public bool grounded;
  14.     Rigidbody rb;
  15.  
  16.     Vector3 from, to;
  17.  
  18.     void Start()
  19.     {
  20.         camera = Camera.main;
  21.         rb = GetComponent<Rigidbody>();
  22.         follow = GameObject.Find("Head").GetComponent<Transform>();
  23.     }
  24.  
  25.     void Update()
  26.     {
  27.         from = camera.transform.position;
  28.         to = follow.position;
  29.         if (Input.GetKeyDown(KeyCode.Space))
  30.         {
  31.             rb.AddForce(new Vector3(0, 1, 0) * 3, ForceMode.Impulse);
  32.         }
  33.         currentSpeed = Mathf.Abs(rb.velocity.magnitude);
  34.     }
  35.  
  36.     private void FixedUpdate()
  37.     {
  38.         float x = Input.GetAxisRaw("Horizontal");
  39.         float y = Input.GetAxisRaw("Vertical");
  40.  
  41.         Quaternion rotation = rb.rotation;
  42.  
  43.         if (Input.GetKey(KeyCode.A))
  44.         {
  45.             transform.Rotate(Vector3.up * -150 * Time.fixedDeltaTime);
  46.         }
  47.  
  48.         if (Input.GetKey(KeyCode.D))
  49.         {
  50.             transform.Rotate(Vector3.up * 150 * Time.fixedDeltaTime);
  51.         }
  52.  
  53.         if (Input.GetKey(KeyCode.W))
  54.         {
  55.             rb.AddForce((follow.forward) * 1000);
  56.         }
  57.         float maxforward = 1f;
  58.         if ((Mathf.Abs(rb.velocity.x) >= maxforward || Mathf.Abs(rb.velocity.z) >= maxforward))
  59.         {
  60.             Vector3 movement = new Vector3(rb.velocity.x >= maxforward ? maxforward : rb.velocity.x, rb.velocity.y, rb.velocity.z >= maxforward ? maxforward : rb.velocity.z).normalized * maxforward;
  61.             rb.velocity = new Vector3(movement.x, rb.velocity.y, movement.z);
  62.         }
  63.  
  64.         if (Input.GetKey(KeyCode.S))
  65.         {
  66.             rb.AddForce((-follow.forward) * 1000);
  67.         }
  68.  
  69.     }
  70.  
  71.     private void OnCollisionStay(Collision collision)
  72.     {
  73.         grounded = true;
  74.     }
  75.  
  76.     private void OnCollisionExit(Collision collision)
  77.     {
  78.         grounded = false;
  79.     }
  80.  
  81.     void LateUpdate()
  82.     {
  83.      
  84.         camera.transform.LookAt(to);
  85.     }
  86.  
  87.     private void OnDrawGizmosSelected()
  88.     {
  89.         Gizmos.color = Color.red;
  90.         Gizmos.DrawLine(from, to);
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement