Search Unity

Weird Controls

Discussion in 'Scripting' started by Glemau, Aug 29, 2015.

  1. Glemau

    Glemau

    Joined:
    Jul 24, 2015
    Posts:
    37
    Hi everyone,

    My Character Controller is acting weird.
    When I look forward it goes forward.
    When I look sideways it goes sideways.
    When I look Backwards it goes Backwards.
    Heres my mouseLook Script(attached to the Camera, Child of my actual player.):

    Code (CSharp):
    1. usingUnityEngine;
    2.  
    3. //VerysimplesmoothmouselookmodifierfortheMainCamerainUnity
    4. //byFrancisR. Griffiths-Keam - www.runningdimensions.com
    5.  
    6. [AddComponentMenu("Camera/Simple Smooth Mouse Look ")]
    7. publicclassMouseLook : MonoBehaviour
    8. {
    9. Vector2_mouseAbsolute;
    10. Vector2_smoothMouse;
    11.  
    12. publicVector2clampInDegrees = newVector2(360, 180);
    13. publicboollockCursor;
    14. publicVector2sensitivity = newVector2(2, 2);
    15. publicVector2smoothing = newVector2(3, 3);
    16. publicVector2targetDirection;
    17. publicVector2targetCharacterDirection;
    18.  
    19. //Assignthisifthere'saparentobjectcontrollingmotion, suchasaCharacterController.
    20. //Yawrotationwillaffectthisobjectinsteadofthecameraifset.
    21. publicGameObjectcharacterBody;
    22.  
    23. voidStart()
    24. {
    25. //Settargetdirectiontothecamera'sinitialorientation.
    26. targetDirection = transform.localRotation.eulerAngles;
    27.  
    28. //Settargetdirectionforthecharacterbodytoitsinitalstate.
    29. if (characterBody) targetCharacterDirection = characterBody.transform.localRotation.eulerAngles;
    30. }
    31.  
    32. voidUpdate()
    33. {
    34. //Ensurethecursorisalwayslockedwhenset
    35. Screen.lockCursor = lockCursor;
    36.  
    37. //Allowthescripttoclampbasedonadesiredtargetvalue.
    38. vartargetOrientation = Quaternion.Euler(targetDirection);
    39. vartargetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);
    40.  
    41. //Getrawmouseinputforacleanerreadingonmoresensitivemice.
    42. varmouseDelta = newVector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
    43.  
    44. //Scaleinputagainstthesensitivitysettingandmultiplythatagainstthesmoothingvalue.
    45. mouseDelta = Vector2.Scale(mouseDelta, newVector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));
    46.  
    47. //Interpolatemousemovementovertimetoapplysmoothingdelta.
    48. _smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
    49. _smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);
    50.  
    51. //Findtheabsolutemousemovementvaluefrompointzero.
    52. _mouseAbsolute += _smoothMouse;
    53.  
    54. //Clampandapplythelocalxvaluefirst, soasnottobeaffectedbyworldtransforms.
    55. if (clampInDegrees.x < 360)
    56. _mouseAbsolute.x = Mathf.Clamp(_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);
    57.  
    58. varxRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.right);
    59. transform.localRotation = xRotation;
    60.  
    61. //Thenclampandapplytheglobalyvalue.
    62. if (clampInDegrees.y < 360)
    63. _mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);
    64.  
    65. transform.localRotation *= targetOrientation;
    66.  
    67. //Ifthere'sacharacterbodythatactsasaparenttothecamera
    68. if (characterBody)
    69. {
    70. varyRotation = Quaternion.AngleAxis(_mouseAbsolute.x, characterBody.transform.up);
    71. characterBody.transform.localRotation = yRotation;
    72. characterBody.transform.localRotation *= targetCharacterOrientation;
    73. }
    74. else
    75. {
    76. varyRotation = Quaternion.AngleAxis(_mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));
    77. transform.localRotation *= yRotation;
    78. }
    79. }
    80. }


    And my movement scripts(attached to the player character):
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour {
    5.  
    6.     public Transform Trans;
    7.     public Rigidbody rb;
    8.     public int moveSpeed;
    9.     public int sprintIncrease;
    10.     public int strafeSpeed;
    11.  
    12.     // Use this for initialization
    13.     void Start ()
    14.     {
    15.         Trans = gameObject.GetComponent<Transform>();
    16.         rb = gameObject.GetComponent<Rigidbody> ();
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.         if (Input.GetKey (KeyCode.W))
    23.         {
    24.             rb.transform.Translate(transform.forward*moveSpeed*Time.deltaTime);
    25.         }
    26.         if (Input.GetKey (KeyCode.D))
    27.         {
    28.             rb.transform.Translate(transform.right*strafeSpeed*Time.deltaTime);
    29.         }
    30.         if (Input.GetKey (KeyCode.A))
    31.         {
    32.             rb.transform.Translate(-transform.right*strafeSpeed*Time.deltaTime);
    33.         }
    34.         if (Input.GetKey (KeyCode.S))
    35.         {
    36.             rb.transform.Translate(-transform.forward*strafeSpeed*Time.deltaTime);
    37.         }
    38.         if (Input.GetKey (KeyCode.LeftShift))
    39.         {
    40.             rb.transform.Translate(transform.forward*moveSpeed*sprintIncrease*Time.deltaTime);
    41.         }
    42.         if (Input.GetKeyDown (KeyCode.Space))
    43.         {
    44.             rb.AddForce(0,500,0);
    45.         }
    46.  
    47.     }
    48. }
    49.  

    Hope You can help.

    ~Glemau
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This must mean I'm up far to late. I'm not seeing the problem.

    Isn't this desired behaviour? Or do you want the player to move in a direction they are not facing?

    A quick description of what should happen will help.

    Edit: pro tip: you are allowed to put spaces in your comments.
     
    Last edited: Aug 29, 2015
    MyIsaak likes this.
  3. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Yeah, that would be great :) It looks like those find the words puzzles
     
  4. Glemau

    Glemau

    Joined:
    Jul 24, 2015
    Posts:
    37
    So I mean the player moves sideways from where i look. So he doesn't move in the direction the camera is looking