Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- public class PlayerController : MonoBehaviour
- {
- Rigidbody rb;
- public float speed = 10.0f;
- Vector3 movement;
- int score = 0;
- public TextMeshProUGUI scoreText;
- public float jumpSpeed = 10.0f;
- public bool canJump = true;
- public bool canDoubleJump = false;
- void Start()
- {
- rb = GetComponent<Rigidbody>();
- scoreText = GameObject.Find("Score").GetComponent<TextMeshProUGUI>();
- }
- void Update()
- {
- movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
- if (Input.GetButtonDown("Jump") && canJump)
- {
- rb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
- }
- else if(Input.GetButtonDown("Jump") && canDoubleJump)
- {
- rb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
- canDoubleJump = false;
- }
- }
- private void FixedUpdate()
- {
- rb.velocity = new Vector3(movement.x, 0, movement.z) * speed + new Vector3(0, rb.velocity.y, 0);
- }
- public void AwardPoints(int points)
- {
- score += points;
- scoreText.text = "Score: " + score;
- }
- private void OnCollisionEnter(Collision collision)
- {
- if(collision.gameObject.tag == "Ground")
- {
- canJump = true;
- }
- }
- private void OnCollisionExit(Collision collision)
- {
- if (collision.gameObject.tag == "Ground")
- {
- canJump = false;
- canDoubleJump = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement