Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Rotation not rotating my player in unity.

Discussion in 'Scripting' started by FaxingStudios, Sep 4, 2023.

  1. FaxingStudios

    FaxingStudios

    Joined:
    Apr 29, 2023
    Posts:
    10
    Okay, I finally found a way to post publicly. That's not what the conversation is about. As I was saying, my player would not respond to my scripts. Is it because I use a character controller to move and a rigid body to collide with triggers? Tell me if that is true, but the character is not responding to rotations.

    Is there any way to help out, Also, thank you who trying to help me out with the forums because I am a newbie to the forums.:D
     
  2. FaxingStudios

    FaxingStudios

    Joined:
    Apr 29, 2023
    Posts:
    10
  3. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I think character controllers use their own methods of modifying transforms. As far as rigidbody, that shouldn't have any issues unless you've locked any constraints on rotations(manually in Inspector).

    I would research into the character controllers methods:
    https://docs.unity3d.com/ScriptReference/CharacterController.html
     
  4. FaxingStudios

    FaxingStudios

    Joined:
    Apr 29, 2023
    Posts:
    10
    Wait, I'm lost. What is going on. "Modifying transforms?" I really don't know, I should look into that.
     
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Yes the objects "Transform" component, is what controls the objects position, rotation, and scale. So technically if you change any of those values, you're modifying it. It's just a way I say it.

    But I think a character controller handles all of that, so it has it's own methods, and it's best to use it's calls. I'm not familiar with Character Controllers, as I don't use them, so you probably know more about them than I do, lol
     
  6. FaxingStudios

    FaxingStudios

    Joined:
    Apr 29, 2023
    Posts:
    10
    Thanks, oh and by the way, a character controller is better for moving instead of a rigid body, rigid bodies require raycasts for grounding, while character controllers just do the simple way, typing "charactercontroller.isgrounded".:rolleyes:
     
    Last edited: Sep 4, 2023
  7. FaxingStudios

    FaxingStudios

    Joined:
    Apr 29, 2023
    Posts:
    10
    I finally did it, it's finally rotating, but, it keeps snapping back to place when idle, what now?
     
    Last edited: Sep 4, 2023
  8. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Everything in code either requires a raycast or collision check to determine "isGrounded". If you have a pre-built method that handles that, it doesn't mean it's not doing the same thing, it's just skipping those steps so you don't see them, or are required to add anything for it. And are things you probably should know to begin with. But to each their own...

    A normal rotation method looks like this:
    Code (CSharp):
    1. Vector3 direction = moveTo - transform.position;
    2.     //direction.y = 0; // needed if not aiming up/down
    3.     Quaternion targetRotation = Quaternion.LookRotation(direction, Vector3.up);
    4.     transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, turnSpeed);
    But I see you want to Slerp, that's a little different:
    Code (CSharp):
    1. transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, timeCount);
    2.         timeCount = timeCount + Time.deltaTime;
    I see you use Quaternion.LookRotation() but you don't add the second axis, as the normal rotation method shows above^. So that may be one of your issues. But the rotation is only going to do what you tell it, so what are you telling it?
     
  9. FaxingStudios

    FaxingStudios

    Joined:
    Apr 29, 2023
    Posts:
    10
    I found another way to do it---

    if (movementDirection != Vector3.zero)
    {
    transform.rotation = Quaternion.LookRotation(movementDirection);
    }

    but the y value of the rotation transform keeps going to zero
     
  10. FaxingStudios

    FaxingStudios

    Joined:
    Apr 29, 2023
    Posts:
    10
    I dunno what I'm telling it, but look at my recent message above, It fixed my problem but one more issue.
     
  11. jlorenzi

    jlorenzi

    Joined:
    May 2, 2021
    Posts:
    273
    Raycasts are a lot more accurate though from what I heard; cause if a character controller is touching a wall for more than 1 frame then it'll think that it's grounded.
     
  12. FaxingStudios

    FaxingStudios

    Joined:
    Apr 29, 2023
    Posts:
    10
    Got it, but rigid bodies are not the problem the problem is rotation's y going back to 0. Any help, please?

    here's the code, again:
    if (movementDirection != Vector3.zero)
    {
    transform.rotation = Quaternion.LookRotation(movementDirection);
    }
     
  13. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    This may still be running, and the Y itself might be going to zero, without the X or Z, so debug it:
    Code (CSharp):
    1. if (movementDirection != Vector3.zero)
    2. {
    3.    print("I am told to rotate still!");
    4.    transform.rotation = Quaternion.LookRotation(movementDirection);
    5. }
    And do a quick test with a quick small movement, and verify this doesn't still run. If it does still run, you'll need a different way to check against.
     
  14. FaxingStudios

    FaxingStudios

    Joined:
    Apr 29, 2023
    Posts:
    10
    it runs when I move, so what does that mean? It still goes back to 0.
     
  15. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    That means another part of the code, somewhere, either isn't setting movement direction to perfect (0,0,0) to stop this from rotating, or you have movement direction to be at a "stand still" while looking in the direction of whatever y=0 is.

    So either find a way to make a perfect (0,0,0) happen(if you still wish to use this check), or use another way to handle when rotation should or should not run.

    You can add more to that debug print, like:
    Code (CSharp):
    1. print($"I am rotating because: {movementDirection} is not (0,0,0)");
     
    FaxingStudios likes this.
  16. FaxingStudios

    FaxingStudios

    Joined:
    Apr 29, 2023
    Posts:
    10
    I read some of your messages
    I looked back at your messages, they helped, so, THANK YOU SO MUCH.
     
    Last edited: Sep 30, 2023