Advertisement
mastercs

unity1

Apr 16th, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. public class PlayerScript : MonoBehaviour
  2. {
  3. public Rigidbody rb;
  4. public Camera cam;
  5. public float movementSpeed;
  6. public float jumpPower;
  7.  
  8. public int coinCount = 0;
  9. public int initialHealth = 3;
  10.  
  11. public int health = 0;
  12.  
  13. private Vector3 spawnPoint;
  14.  
  15. // Start is called before the first frame update
  16. private void Start()
  17. {
  18. spawnPoint = transform.position;
  19. health = initialHealth;
  20. }
  21.  
  22. public bool OnGround()
  23. {
  24. return Physics.Raycast(transform.position - new Vector3(0, .49f, 0), Vector3.down, .02f);
  25. }
  26.  
  27. public void Die()
  28. {
  29. health = initialHealth;
  30. transform.position = spawnPoint;
  31. }
  32.  
  33. // Update is called once per frame
  34. private void Update()
  35. {
  36. float rotationDegrees = Mathf.Atan2(Input.GetAxis("Vertical"), -Input.GetAxis("Horizontal")) * 180f / Mathf.PI - 90;
  37. Vector3 facingDirection = Camera.main.transform.forward;
  38.  
  39. Vector2 newHorizontalVelocity = new Vector2(0, 0);
  40. float newVerticalVelocity = rb.velocity.y;
  41. if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1f || Mathf.Abs(Input.GetAxis("Horizontal")) > 0.1f)
  42. {
  43. Vector3 movementVelocity = Quaternion.AngleAxis(rotationDegrees, Vector3.up) * facingDirection * movementSpeed;
  44. newHorizontalVelocity = new Vector2(movementVelocity.x, movementVelocity.z);
  45. }
  46.  
  47. if (Input.GetKeyDown("space") && OnGround())
  48. {
  49. newVerticalVelocity += jumpPower;
  50. }
  51.  
  52. rb.velocity = new Vector3(newHorizontalVelocity.x, newVerticalVelocity, newHorizontalVelocity.y);
  53.  
  54. if (transform.position.y < -5 || health <= 0)
  55. {
  56. Die();
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement