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 PlayerMovement : MonoBehaviour
- {
- int MoveSpeed = 1;
- float MaxSpeed = 5f;
- Rigidbody rb;
- Transform ts;
- // Start is called before the first frame update
- void Start()
- {
- rb = gameObject.GetComponent<Rigidbody>();
- ts = gameObject.GetComponent<Transform>();
- }
- // Update is called once per frame
- void FixedUpdate()
- {
- float XMove = Input.GetAxisRaw("Horizontal");
- float YMove = Input.GetAxisRaw("Vertical");
- rb.AddForce(XMove * MoveSpeed, 0, YMove * MoveSpeed, ForceMode.VelocityChange);
- if (rb.velocity.magnitude > MaxSpeed)
- {
- float x = rb.velocity.magnitude / MaxSpeed;
- rb.velocity /= x;
- }
- }
- void OnTriggerEnter(Collider collision)
- {
- if(collision.gameObject.tag == "Finish")
- {
- MoveSpeed = 0;
- rb.velocity = new Vector3(0, 0, 0);
- StartCoroutine(EndPointLerp(collision.gameObject.GetComponent<Transform>().position));
- }
- }
- IEnumerator EndPointLerp(Vector3 EndPointPos)
- {
- while (ts.position != EndPointPos)
- {
- float EndPointLerpSpeed = Vector3.Distance(ts.position, EndPointPos) * 3;
- ts.position = Vector3.Lerp(ts.position, EndPointPos, 0.5f * Time.deltaTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement