Search Unity

How to Hover player and un-Hover rigidbody player correctly?

Discussion in 'Scripting' started by ynm11, Jul 9, 2021.

  1. ynm11

    ynm11

    Joined:
    Jul 6, 2021
    Posts:
    57
    I'm trying to write a code with logic:

    - if Player presses Space they auto-jump towards a Cube and Hover at Cube position.

    - if Player presses ALT they drop from the Cube and fall back to Ground.

    Both Player and Cube have Rigidbodies. I want Player to hover at Cube position when it reaches cube position.

    The code I'm using for Player to auto-jump to Cube position is
    rb.AddForce((cube.transform.position - rb.transform.position) * forceMultiply);


    The problem is that sometimes when Player jumps towards Cube at a weird angle, a collision occurs that sends Player flying way past the cube.

    Currently I am trying to remedy this by doing code that if Player is within x distance of Cube, then
    PlayerRigidbody.useGravity = false
    and
    PlayerRigidbody.Velocity = Vector3.zero


    However, when I press ALT (with intended goal that Player stops hovering and falls back to ground), Player falls *some* but does not fully reach ground.

    The code for when Player presses ALT is currently
    PlayerRigidbody.useGravity = true
    and
    PlayerRigidbody.velocity = new Vector3(0, -500, 0);


    but yeah, that doesn't fully work. Does anyone know how to successfully create a Hover effect and also how to successfully *cancel* a Hover effect so that Player fully falls back to ground with functioning gravity and velocity again?
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,648
    On your RigidBody you could just use the Constraints > Freeze Position > Y value to stop it from falling, and then disable it afterwards.
     
    ynm11 likes this.
  3. ynm11

    ynm11

    Joined:
    Jul 6, 2021
    Posts:
    57
    That sounds like a cool approach!
    I found this code for it, but when I put it in, I get an error:
    "the contextual keyword 'var' may only appear within a logical variable declaration or in script code"...

    Code (CSharp):
    1. var originalConstraints : RigidbodyConstraints;
    2. function Awake()
    3. {
    4.       originalConstraints = rigidbody.constraints;
    5. }
    6. function FreezeConstraints()
    7. {
    8.       rigidbody.constraints = RigidbodyConstraints.FreezePositionY;
    9. }
    10. function UnFreezeConstraints()
    11. {
    12.       rigidbody.constraints = originalConstraints;
    13. }
    Do you know how this is supposed to be place into the code properly so that Unity likes it lol?
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,648
    That looks really old, probably when Unity was still using UnityScript. You're going to have to convert it from JavaScript to C#.

    If you can't convert this to C# on your own, I'd suggest you watch some tutorial videos or take a class on programming, as it's going to get a lot harder from here on, but if you're going to muster on anyways, here's that code but in C#.

    Code (CSharp):
    1. RigidbodyConstraints originalConstraints;
    2. void Awake()
    3. {
    4.       originalConstraints = rigidbody.constraints;
    5. }
    6. private void FreezeConstraints()
    7. {
    8.       rigidbody.constraints = RigidbodyConstraints.FreezePositionY;
    9. }
    10. private void UnFreezeConstraints()
    11. {
    12.       rigidbody.constraints = originalConstraints;
    13. }
     
    ynm11 likes this.