0
\$\begingroup\$

I'm using a raycast controller and kinematic ridigbody.

The way I've got my project set up is that I have a set of default values, such as gravity, jump height, etc., and when I enter a certain trigger, such as water, these values change (or, more specifically, I override these values temporarily).

My velocity.y += gravity * time.deltaTime, but there are some bugs if I jump into the water. For example, if I jump in from above then the velocity.y does not change in the water until I collide with the bottom of the water. Essentially it looks like I'm performing a normal jump, and only once I land will the changed gravity in the water trigger affect my movement. Also, if I jump in the water trigger from the side it sometimes propels me very high in the air.

When I print out the gravity value it does change immediately when I enter the water, but it seems the downward force from falling is never changed until I hit the bottom of the ocean floor.

public ControllerParameters2D Parameters { get { return overrideParameters 
   ?? defaultParameters; } } //Returns updated parameters, or default 
                             //parameters if overrideParameters is null

public void LateUpdate()
{
    JumpInSeconds -= Time.deltaTime; //Calculates how long until the player can jump again

    velocity.y += Parameters.gravity * Time.deltaTime; //Adds the force of gravity to the character's y velocity, scaled per time

    Move(Velocity * Time.deltaTime); //Moves the character per its velocity, scaled by time
}

public void OnTriggerEnter2D(Collider2D other)
{
    var newParameters = other.gameObject.GetComponent<ControllerPhysicsVolume2D>();

    if (newParameters == null)
    {
        return;
    }

    overrideParameters = newParameters.NewParameters;
}

public void OnTriggerExit2D(Collider2D other)
{
    var newParameters = other.gameObject.GetComponent<ControllerPhysicsVolume2D>();

    if (newParameters == null)
    {
        return;
    }

    overrideParameters = null;
}
\$\endgroup\$
  • \$\begingroup\$ Any reason why velocity.y = 0 isn't acceptable? \$\endgroup\$ – Jay Jan 22 '18 at 3:20
  • 1
    \$\begingroup\$ This worked. Can I tell you, I've spent nearly 60 hours this week trying to perfect a ladder system, and decided to take a break and try this today. I've been so mentally overwhelmed with the ladders that I guess I just didn't think of this very obvious solution myself...Thank you very much! \$\endgroup\$ – Ginger and Lavender Jan 22 '18 at 3:32
  • \$\begingroup\$ Do you have separate time deltas for the different portions of the jump (time spent in air, time spent in water)? \$\endgroup\$ – Acccumulation Jan 22 '18 at 19:18
1
\$\begingroup\$

One way to do that is to use vector2.scale().

This scales every value of a vector by a respective value of another vector. So you could say:

OnTriggerEnter2D(Collider2D){
  velocity = Vector2.Scale(velocity, new Vector2(1,0));
}

You can also do a check to see if the velocity's y value is negative (which means you're falling), then do a ontriggerstay2d check to see if he's still in the water, and scale the vector by a float. this way his velocity will change over time while he's in the water. make sure you don't set these values in update, or lateUpdate.

I also think the reason he doesn't slow down when he enters the water is because you might be setting the gravity in the update function. Set it in the start or awake function, and change it in ontriggerenter and ontriggerexit.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.