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 Player : MonoBehaviour
- {
- public Camera camera;
- [SerializeField]
- private Transform follow;
- private Vector3 targetPosition;
- public float currentSpeed;
- public bool grounded;
- Rigidbody rb;
- Vector3 from, to;
- void Start()
- {
- camera = Camera.main;
- rb = GetComponent<Rigidbody>();
- follow = GameObject.Find("Head").GetComponent<Transform>();
- }
- void Update()
- {
- from = camera.transform.position;
- to = follow.position;
- if (Input.GetKeyDown(KeyCode.Space))
- {
- rb.AddForce(new Vector3(0, 1, 0) * 3, ForceMode.Impulse);
- }
- currentSpeed = Mathf.Abs(rb.velocity.magnitude);
- }
- private void FixedUpdate()
- {
- float x = Input.GetAxisRaw("Horizontal");
- float y = Input.GetAxisRaw("Vertical");
- Quaternion rotation = rb.rotation;
- if (Input.GetKey(KeyCode.A))
- {
- transform.Rotate(Vector3.up * -150 * Time.fixedDeltaTime);
- }
- if (Input.GetKey(KeyCode.D))
- {
- transform.Rotate(Vector3.up * 150 * Time.fixedDeltaTime);
- }
- if (Input.GetKey(KeyCode.W))
- {
- rb.AddForce((follow.forward) * 1000);
- }
- float maxforward = 1f;
- if ((Mathf.Abs(rb.velocity.x) >= maxforward || Mathf.Abs(rb.velocity.z) >= maxforward))
- {
- 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;
- rb.velocity = new Vector3(movement.x, rb.velocity.y, movement.z);
- }
- if (Input.GetKey(KeyCode.S))
- {
- rb.AddForce((-follow.forward) * 1000);
- }
- }
- private void OnCollisionStay(Collision collision)
- {
- grounded = true;
- }
- private void OnCollisionExit(Collision collision)
- {
- grounded = false;
- }
- void LateUpdate()
- {
- camera.transform.LookAt(to);
- }
- private void OnDrawGizmosSelected()
- {
- Gizmos.color = Color.red;
- Gizmos.DrawLine(from, to);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement