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 PlayerInput : MonoBehaviour
- {
- public GameObject Barrier1;
- public GameObject Barrier2;
- private Animator anim;
- private Rigidbody rb;
- private float RunningSpeed = 8.5f;
- private float BackwardSpeed = 1.0f;
- private float IdleSpeed = 3.5f;
- private bool CurRunning = false;
- private bool IsGrounded = true;
- private float JumpHeight = 24.0f;
- public float MovementSpeed;
- // Start is called before the first frame update
- void Start()
- {
- anim = GetComponent<Animator>();
- rb = GetComponent<Rigidbody>();
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetKey("escape"))
- {
- Application.Quit();
- }
- if (Input.GetKey("d"))
- {
- MovementSpeed = 1 * Time.deltaTime * RunningSpeed;
- transform.Translate(MovementSpeed, 0, 0);
- if(anim.GetBool("Move") == false)
- {
- StartRunAnimation();
- }
- CurRunning = true;
- }
- else if (Input.GetKey("a"))
- {
- MovementSpeed = 1 * Time.deltaTime * BackwardSpeed;
- transform.Translate(MovementSpeed, 0, 0);
- if(anim.GetBool("Move") == true)
- {
- StopRunAnimation();
- }
- CurRunning = false;
- }
- else
- {
- MovementSpeed = 1 * Time.deltaTime * IdleSpeed;
- transform.Translate(MovementSpeed, 0, 0);
- if (anim.GetBool("Move") == true)
- {
- StopRunAnimation();
- }
- CurRunning = false;
- }
- if (Input.GetKeyDown("space") && IsGrounded)
- {
- rb.AddForce(new Vector3(0, JumpHeight, 0), ForceMode.Impulse);
- IsGrounded = false;
- if (Input.GetKey("d"))
- {
- anim.Play("RunningToJump");
- }
- else
- {
- anim.Play("IdleToJump");
- }
- }
- }
- void StartRunAnimation()
- {
- anim.SetBool("Move", true);
- }
- void StopRunAnimation()
- {
- anim.SetBool("Move", false);
- }
- private void OnCollisionEnter(Collision collision)
- {
- if(collision.collider.gameObject.name != "GrassPlatform" && collision.collider.gameObject != Barrier1 && collision.collider.gameObject != Barrier2)
- {
- if(CurRunning == true)
- {
- anim.Play("HitWhileRunning");
- }
- else
- {
- anim.Play("GetHit");
- }
- }
- else
- {
- IsGrounded = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement