Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5.  
  6. public class PlayerController : MonoBehaviour
  7. {
  8. public float moveSpeed;
  9. public float jumpForce;
  10.  
  11. private Rigidbody rig;
  12.  
  13. void Awake()
  14. {
  15. // get the components
  16. rig = GetComponent<Rigidbody>();
  17. }
  18.  
  19. void Update()
  20. {
  21. Move();
  22.  
  23. if (Input.GetButtonDown("Jump"))
  24. {
  25. TryJump();
  26. }
  27. }
  28.  
  29. void Move()
  30. {
  31. // getting our inputs
  32. float xInput = Input.GetAxis("Horizontal");
  33. float zInput = Input.GetAxis("Vertical");
  34.  
  35. Vector3 dir = new Vector3(xInput, 0, zInput) * moveSpeed;
  36. dir.y = rig.velocity.y;
  37.  
  38. rig.velocity = dir;
  39.  
  40. Vector3 facingDir = new Vector3(xInput, 0, zInput);
  41.  
  42. if (facingDir.magnitude > 0)
  43. {
  44. transform.forward = facingDir;
  45. }
  46. }
  47.  
  48. void TryJump ()
  49. {
  50. Ray ray1 = new Ray(transform.position + new Vector3(0.5f, 0, 0.5f), Vector3.down);
  51. Ray ray2 = new Ray(transform.position + new Vector3(-0.5f, 0, 0.5f), Vector3.down);
  52. Ray ray3 = new Ray(transform.position + new Vector3(0.5f, 0, -0.5f), Vector3.down);
  53. Ray ray4 = new Ray(transform.position + new Vector3(-0.5f, 0, -0.5f), Vector3.down);
  54.  
  55. bool cast1 = Physics.Raycast(ray1, 0.7f);
  56. bool cast2 = Physics.Raycast(ray2, 0.7f);
  57. bool cast3 = Physics.Raycast(ray3, 0.7f);
  58. bool cast4 = Physics.Raycast(ray4, 0.7f);
  59.  
  60. if (cast1 || cast2 || cast3 || cast4)
  61. {
  62. rig.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement