My PlayerMovement script isn’t working

Updated on July 22, 2018 in  [R] Scripts
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on July 22, 2018

It moves around based on the worlds x, y, z and not on its own rotation but I don’t know how to fix it.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]

public class PlayerMovement : MonoBehaviour {

  1. public Rigidbody rb;
  2. public float jumpHeight = 10f;
  3. public bool isGrounded;
  4. public float walkingSpeed = 0.1f;
  5. public float runningSpeed = 10f;
  6. float speed;
  7. public GameObject eyes;
  8. public float sensitivity;
  9. float rotX;
  10. float rotY;
  11. void Start ()
  12. {
  13. rb = GetComponent<Rigidbody>();
  14. speed = walkingSpeed;
  15. isGrounded = true;
  16. }
  17. void Update ()
  18. {
  19. if (Input.GetKey(KeyCode.LeftShift))
  20. {
  21. speed = runningSpeed;
  22. }
  23. else
  24. {
  25. speed = walkingSpeed;
  26. }
  27. float moveLR = Input.GetAxis("Horizontal") * speed;
  28. float moveFB = Input.GetAxis("Vertical") * speed;
  29. rotX = Input.GetAxis("Mouse X") * sensitivity;
  30. rotY = Input.GetAxis("Mouse Y") * sensitivity;
  31. Vector3 movement = new Vector3(moveLR, 0, moveFB);
  32. transform.position = transform.position + movement * Time.deltaTime;
  33. transform.Rotate(0, rotX, 0);
  34. eyes.transform.Rotate(-rotY, 0, 0);
  35. if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
  36. {
  37. rb.AddForce(0, jumpHeight, 0);
  38. isGrounded = false;
  39. }
  40. }

}

  • Liked by
Reply