TMoaw
TMoaw

Reputation: 1

handling player impact in a proper way

hi i have a quite a complicated question for me at least . in my player controller class i use unity's characterController component to move the player based of the input i give it to it and multiply it with speed and add the gravity like this

        CurrentImpact = vector3.lerp(CurrentImpact,vector3.zero, damping * Time.DeltaTime) 
        velocity = InputDirection();
        velocity *= Speed();
        velocity += Gravity;
        velocity += CurrentImpact;
        CharcerController.move(velocity  * Time.deltatime)

my problem is with CurrentImpact. CurrentImpact is a vector3 that i use to add force to the player like jumping and pushing the player around put if i do like adding a force facing left maybe and the player is trying to move right, the player will go right then will start drifting a little bit on the left again so what i think i want is a way to make the CurrentImpact Face the direction of the input movement if its weaker than it without changing its magnitude.

i hope you understand me and thanks

Upvotes: 0

Views: 40

Answers (1)

derHugo
derHugo

Reputation: 90813

what think i want is a way to make the CurrentImpact Face the direction of the input movement if its weaker than it without changing its magnitude.

Not sure if it solves your actual problem but I think you could do that by using something like

CurrentImpact = vector3.lerp(CurrentImpact, vector3.zero, damping * Time.DeltaTime);
var inputDirection = InputDirection();
var speed = Speed();

velocity = inputDirection * speed;

var impactMagnitude = CurrentImpact.magnitude;

// Check if your impact is weaker then the movement (without gravity I guess - otherwise do it after adding it)
if(impactMagnitude < speed)
{
    CurrentImpact = inputDirection * impactMagnitude;
}

velocity += Gravity;
velocity += CurrentImpact;
CharcerController.move(velocity  * Time.deltatime);

Upvotes: 0

Related Questions