Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[solved?]Changing code to higher in script making it function properly but other things break...??

Discussion in 'Scripting' started by Internetpolice, Dec 2, 2017.

  1. Internetpolice

    Internetpolice

    Joined:
    Oct 16, 2017
    Posts:
    60
    So i'm using the character controller and have set standards for when to apply gravity and normalized input into controller.move but since adding a new movement command I am getting stuff totally out of sync for some reason...

    here's what my code looks like for the two situations...
    Code (csharp):
    1. Void Update()
    2. {
    3. float yStore = moveDirection.y;
    4.         moveDirection.y = yStore;
    5.         moveDirection = (transform.forward * Input.GetAxis("Vertical") + (transform.right * Input.GetAxis("Horizontal")));
    6.         moveDirection = moveDirection.normalized * moveSpeed;
    7.         if (moveDirection.y > -20)
    8.         {
    9.             moveDirection.y += (Physics.gravity.y * Time.deltaTime * gravityScale);
    10.         }
    11. controller.Move(moveDirection * Time.deltaTime);
    12. if (Input.GetKeyDown("d"))
    13. {
    14. ButtonCooler = 2.0f;      
    15. ButtonCount += 1;
    16. if (ButtonCount = 2)
    17. {
    18. moveDirection = transform.forward * rollDist * moveSpeed;
    19. ButtonCount = 0;
    20. }
    21. }
    22. }
    23.  
    now...
    When I switched this last piece...
    Code (csharp):
    1. if (Input.GetKeyDown("d"))
    2. {
    3. ButtonCooler = 2.0f;      
    4. ButtonCount += 1;
    5. if (ButtonCount = 2)
    6. {
    7. moveDirection = transform.forward * rollDist * moveSpeed;
    8. ButtonCount = 0;
    9. }
    10. }
    to above the line...
    Code (csharp):
    1. controller.Move(moveDirection * Time.deltaTime);
    this causes the transform forward to work but then breaks my gravity for some reason...

    Please can you Help? I think this comes down to how I am ordering these operations and for some reason my brain isn't clicking onto the "proper" order to do these functions.

    I tried moving this d button piece around and it doesn't change the fact that gravity remains broken, it actually causes more issues with movement....

    Even just a general explanation on how the character controller is taking input on .move would help...
    I have read this https://docs.unity3d.com/ScriptReference/CharacterController.Move.html
    but it doesn't cover what i'm asking for or maybe i'm misunderstanding....
     
    Last edited: Dec 2, 2017
  2. Internetpolice

    Internetpolice

    Joined:
    Oct 16, 2017
    Posts:
    60
    So I have no idea why this fixed my issue but in my infinite wisdom I had switched moveDirection.y = yStore; to above the moveDirection code when I also switched the "d" function under the assumption I was compact info together correctly..

    my best guess is I was making moveDirection.y a value to be normalized even though I was trying to add to it after that fact???

    I'm not even sure i'm 100% comprehending why this fixed my issue but hey.. works now..