Advertisement
Guest User

Untitled

a guest
May 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7.  
  8. public float walkSpeed;
  9.  
  10. Rigidbody rb;
  11.  
  12. Vector3 moveDirection;
  13. // Use this for initialization
  14.  
  15. void Awake()
  16. {
  17. rb = GetComponent<Rigidbody>();
  18. }
  19. void Start()
  20. {
  21.  
  22. }
  23.  
  24. // Update is called once per frame
  25. void Update()
  26. {
  27. if (Input.GetKeyDown(KeyCode.Space))
  28. Jump();
  29. float horizontalMovement = Input.GetAxisRaw("Horizontal");
  30. float verticalMovement = Input.GetAxisRaw("Vertical");
  31.  
  32. moveDirection = (horizontalMovement * transform.right + verticalMovement * transform.forward).normalized;
  33. }
  34.  
  35. void FixedUpdate()
  36. {
  37. Move();
  38. }
  39.  
  40. void Move()
  41. {
  42. Vector3 yVelFix = new Vector3(0, rb.velocity.y, 0);
  43. rb.velocity = moveDirection * walkSpeed * Time.deltaTime;
  44. rb.velocity += yVelFix;
  45. }
  46.  
  47. void Jump()
  48. {
  49. rb.AddForce(new Vector3(0, 8, 0), ForceMode.Impulse);
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement