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. Dismiss Notice

Question Should I put all my different forms of movement in the same script?

Discussion in 'Scripting' started by TheProcessYet_64, Apr 27, 2023.

  1. TheProcessYet_64

    TheProcessYet_64

    Joined:
    Jan 4, 2023
    Posts:
    42
    I am using the in-built character controller and I have my standard PlayerMovement script attached to my player that handles walking, running, jumping and so on.

    I have a Water script that is also attached to my player just for experimenting and in that script, I use Controller.Move() and velocity + gravity for my movement. So far considering it's probably not the right way to do it, it works amazing and achieves almost everything I want with water movement.

    But there's a problem with how I do it. Currently, when I enter the trigger area of the water, I disable my PlayerMovement script and then enable my Water script. After I exit the water trigger, my Water script is disabled and my PlayerMovement script is enabled.

    But when I use this approach, when I exit the water on the y axis (basically I'm floating above the water), my character bounces up and down extremely fast when I continue to move up at the edge of the water trigger and my two scripts continuously disable/enable.

    So basically for this scenario and others, is it OK if I just alter my water script and just add it to my main PlayerMovement script? My only concern is that I don't want my main script to have 500+ lines and look messy and unreadable which is why I kept the two scripts (and others) separate. But it's causing an issue I can't fix.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Code organization questions aside - presumably if you combine your scripts you will need to maintain a variable like:
    Code (CSharp):
    1. bool isInWater;
    And based on this variable you will likely run different code in your combined script, right? Given that this is the case, I don't see how combining the scripts will solve any problem for you whatsoever. What am I missing?
     
  3. TheProcessYet_64

    TheProcessYet_64

    Joined:
    Jan 4, 2023
    Posts:
    42
    I'm not sure if this helps but: https://imgur.com/a/iI97ldR that's the effect when I continue to press/hold space bar (which is supposed to move upwards) as I am at the edge of the trigger. In my Debug, while my character is bouncing as shown in the gif, my water & player movement scripts are both constantly disabling/enabling.

    My post was basically trying to find out if that problem will go away if I was to combine my code, because then I would not have to disable any scripts, if that makes sense.

    Code (CSharp):
    1.  
    2. // water
    3. Vector3 move = transform.forward * z + transform.right * x;
    4.  
    5. if (Input.GetKey(KeyCode.Space))
    6.             {
    7.                 move.y = -gravity;
    8.                 controller.Move(move * Time.deltaTime);
    9.             }
    That is basically my space code in my Water script, and in my PlayerMovement script, its a standard:
    Code (CSharp):
    1. if (Input.GetButtonDown("Jump") && !isSliding)
    2.             {
    3.                 velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    4.             }
     
    Last edited: Apr 27, 2023
  4. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    729
    Do you want the player to be able to jump out of the water?

    If no: You could check that you are in water before applying the upward movement.
    If yes: Apply the jump velocity when reaching the water surface to avoid the rapid up/down behaviour you are seeing

    You can get this information from the bounds of the collider.
     
  5. TheProcessYet_64

    TheProcessYet_64

    Joined:
    Jan 4, 2023
    Posts:
    42
    Yeah I've messed with collider bounds for a while and I even had read-only collider bounds code in my script to try and get some use out of that but have had no success.

    I don't want the player to be able to jump out of the water so I tried using collider bounds to create a height limit essentially but that didn't work either.

    My space bar code above is also inside my isInWater bool currently, but I don't think that has made a difference. When I also enter the trigger (via OnTriggerEnter), my IsInWater bool becomes true.
     
  6. StarBornMoonBeam

    StarBornMoonBeam

    Joined:
    Mar 26, 2023
    Posts:
    209
    Try setting your velocity to a constant when at the surface of the water. Also make his y pos constant unless a dive button is pressed. Think about a transition between underwater and surface. I'm not saying this is how to do it, but alot of games have a different state at the surface. The player might be fixed in surface swim stance, and then dive to go underwater. Transition one way using button transition back using Y pos.

    Older first person character swimmers drop into the water there is a delay before the water collider brakes them into a underwater floating state. So they get some depth. Though the surface bounce was still common on those basic controllers. You may want them to drop into the water half collider. And then drop below if he pressed crouch.

    I think math square root is abit over kill for what you need. Jumping may also conflict at the surface. Then maybe the parameter for jumping at the surface needs to include collision with a ledge while swimming.
     
  7. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    729
    That approach should work. Want to share your code?
     
  8. TheProcessYet_64

    TheProcessYet_64

    Joined:
    Jan 4, 2023
    Posts:
    42
    I got rid of it, and all I have left is this read-only code:
    Code (CSharp):
    1. Collider m_Collider;
    2.     Vector3 m_Size, m_Min, m_Max;
    3.  
    4. Debug.Log("Collider size : " + m_Size);
    5.             Debug.Log("Collider bound Minimum : " + m_Min);
    6.             Debug.Log("Collider bound Maximum : " + m_Max);
    What I can remember trying though is I believe using m_Min and m_Max inside of my Input if statement to create a ceiling.
     
  9. TheProcessYet_64

    TheProcessYet_64

    Joined:
    Jan 4, 2023
    Posts:
    42
    Thanks for the reply. My only issue with this is that because I'm inside of a trigger area, I can't detect (or at least am unsure) how high my player is. I have tried to set a limit to how high my player object can go while inside the trigger but unfortunately I can't remember the code I used because it was a few weeks ago now. All I know is that even with several variations of trying to set my velocity to a constant, it would ground me immediately (to the bottom of the water trigger) and I would not be able to use space bar at all, so I had to remove it.

    Edit: I have tried something else aswell, even if I reduce the height + collider of my water so that it is almost completely flat and my player object is always touching the ground (whilst in the water), my character still does that rapid bounce movement just like in my gif.
     
  10. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    729

    Once you have a reference to the collider from OnTriggerEnter you can use:
    Code (CSharp):
    1. m_Collider.bounds.max.y
    To get the world y position.

    https://docs.unity3d.com/ScriptReference/Bounds.html
     
  11. TheProcessYet_64

    TheProcessYet_64

    Joined:
    Jan 4, 2023
    Posts:
    42
    Yeah I remember doing this a while back, my max bounds were listed as (0.50, 2.00, 0.50) for this particular water object. My issue was creating the bounds. https://docs.unity3d.com/ScriptReference/Bounds-ctor.html I used that line of code in there and adjusted it based on my requirements but there was no results.
     
  12. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    729
    You don't need to create the bounds, they are a property of the collider.
     
  13. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    You should check new input mode in Unity , it is much better.
     
  14. icauroboros

    icauroboros

    Joined:
    Apr 30, 2021
    Posts:
    99
    If your player movement system gonna be use more than 2 states (like walk, crouch, swim, fly, drive etc) Then I suggest use state machine pattern. That will enable to split state logic on separate scripts and and will enable to easier debugging. https://gameprogrammingpatterns.com/state.html