Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class playercontroller : MonoBehaviour
  6. {
  7. public GameObject hitbox;
  8. private Rigidbody rb;
  9. private Vector3 totalVelocity;
  10. public float speed = 100;
  11. // Start is called before the first frame update
  12. void Start()
  13. {
  14. rb = GetComponent<Rigidbody>();
  15. rb.freezeRotation = true;
  16. Vector2 playerDirectionalInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
  17. }
  18.  
  19. // Update is called once per frame
  20. void FixedUpdate()
  21. {
  22. //setting velocity to zero, then calculating velocity depending on arrow keys
  23. //sum of velocity is then set to rigidbody
  24. totalVelocity = Vector3.zero;
  25. if (Input.GetKey(KeyCode.RightArrow))
  26. {
  27. totalVelocity += transform.right * speed * Time.deltaTime;
  28. }
  29. if (Input.GetKey(KeyCode.LeftArrow))
  30. {
  31. totalVelocity -= transform.right * speed * Time.deltaTime;
  32. }
  33. if(Input.GetKey(KeyCode.UpArrow))
  34. {
  35. totalVelocity += transform.forward * speed * Time.deltaTime;
  36. }
  37. if (Input.GetKey(KeyCode.DownArrow))
  38. {
  39. totalVelocity -= transform.forward * speed * Time.deltaTime;
  40. }
  41. if (totalVelocity != Vector3.zero)
  42. {
  43. //rotate velocity to be relative to camera
  44. totalVelocity = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y, 0) * totalVelocity;
  45. //rotate character to be relative to velocity
  46. transform.rotation = Quaternion.LookRotation(totalVelocity, Vector3.up);
  47. }
  48. //setting rb velocity
  49. rb.velocity = totalVelocity;
  50.  
  51. //set hitbox active depending on spacebar
  52. if(Input.GetKey(KeyCode.Space))
  53. {
  54. hitbox.SetActive(true);
  55. }
  56. else
  57. {
  58. hitbox.SetActive(false);
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement