Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PlayerScript : MonoBehaviour
- {
- public Rigidbody rb;
- public Camera cam;
- public float movementSpeed;
- public float jumpPower;
- public int coinCount = 0;
- public int initialHealth = 3;
- public int health = 0;
- private Vector3 spawnPoint;
- // Start is called before the first frame update
- private void Start()
- {
- spawnPoint = transform.position;
- health = initialHealth;
- }
- public bool OnGround()
- {
- return Physics.Raycast(transform.position - new Vector3(0, .49f, 0), Vector3.down, .02f);
- }
- public void Die()
- {
- health = initialHealth;
- transform.position = spawnPoint;
- }
- // Update is called once per frame
- private void Update()
- {
- float rotationDegrees = Mathf.Atan2(Input.GetAxis("Vertical"), -Input.GetAxis("Horizontal")) * 180f / Mathf.PI - 90;
- Vector3 facingDirection = Camera.main.transform.forward;
- Vector2 newHorizontalVelocity = new Vector2(0, 0);
- float newVerticalVelocity = rb.velocity.y;
- if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1f || Mathf.Abs(Input.GetAxis("Horizontal")) > 0.1f)
- {
- Vector3 movementVelocity = Quaternion.AngleAxis(rotationDegrees, Vector3.up) * facingDirection * movementSpeed;
- newHorizontalVelocity = new Vector2(movementVelocity.x, movementVelocity.z);
- }
- if (Input.GetKeyDown("space") && OnGround())
- {
- newVerticalVelocity += jumpPower;
- }
- rb.velocity = new Vector3(newHorizontalVelocity.x, newVerticalVelocity, newHorizontalVelocity.y);
- if (transform.position.y < -5 || health <= 0)
- {
- Die();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement