Advertisement
Guest User

Error

a guest
Jul 12th, 2021
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.44 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7.     int MoveSpeed = 1;
  8.     float MaxSpeed = 5f;
  9.  
  10.     Rigidbody rb;
  11.     Transform ts;
  12.  
  13.     // Start is called before the first frame update
  14.     void Start()
  15.     {
  16.         rb = gameObject.GetComponent<Rigidbody>();
  17.         ts = gameObject.GetComponent<Transform>();
  18.     }
  19.  
  20.     // Update is called once per frame
  21.     void FixedUpdate()
  22.     {
  23.         float XMove = Input.GetAxisRaw("Horizontal");
  24.         float YMove = Input.GetAxisRaw("Vertical");
  25.  
  26.         rb.AddForce(XMove * MoveSpeed, 0, YMove * MoveSpeed, ForceMode.VelocityChange);
  27.  
  28.         if (rb.velocity.magnitude > MaxSpeed)
  29.         {
  30.             float x = rb.velocity.magnitude / MaxSpeed;
  31.             rb.velocity /= x;
  32.         }
  33.     }
  34.  
  35.     void OnTriggerEnter(Collider collision)
  36.     {
  37.         if(collision.gameObject.tag == "Finish")
  38.         {
  39.             MoveSpeed = 0;
  40.             rb.velocity = new Vector3(0, 0, 0);
  41.             StartCoroutine(EndPointLerp(collision.gameObject.GetComponent<Transform>().position));
  42.         }
  43.     }
  44.  
  45.     IEnumerator EndPointLerp(Vector3 EndPointPos)
  46.     {
  47.         while (ts.position != EndPointPos)
  48.         {
  49.             float EndPointLerpSpeed = Vector3.Distance(ts.position, EndPointPos) * 3;
  50.             ts.position = Vector3.Lerp(ts.position, EndPointPos, 0.5f * Time.deltaTime);
  51.         }
  52.     }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement