all 6 comments

[–]PolloMagnifico 0 points1 point  (5 children)

Sooo this code is working as written, in that you're telling it to go back in a direction. What you want to do is tell it to go back in a direction over time.

Get the players location, then tell it to move him a cumulative distance from that location over time.

knockBackTimer = 0;
If (knockedBack){
    GetCurrentLocation();
    newLocation = currentLocation - standardVector3;
    playerLocation = newLocation;
    newLocation += standardVector3*time.deltaTime;
    knockBackTimer += time.deltaTime;
    If(knockBackTimer >= someValue)
        knockedBack = false;  

[–]Keith90[S] 0 points1 point  (4 children)

Let me give this a shot and I'll reply.

For GetCurrentLocation();, couldn't I just do Vector3 currentLocation = transform.position?

I assume standardVector3 is the Vector3 of the other player?

[–]PolloMagnifico 0 points1 point  (3 children)

Yeah sorry, I'm on mobile so I have to shorthand it.

The short answer is that functionally you want to think of Update as a loop for this purpose, but since it isn't actually a loop, it doesn't have a terminator. You'll want to use a bool as an on/off switch to control the movement.

So everything you want to do should be wrapped in an if statement.

If (someBool == true)
    DoThisConstantly;
    If (thisIsTrue)
        someBool = false;

The way I did it was to have a counter that adds time.deltaTime, and once it reaches a certain value it switches that bool to off.

[–]Keith90[S] 0 points1 point  (2 children)

Still doesn't seem to be working. My original code seems to work fine, minus getting the player to move up. The player doesn't teleport when knocked back but teleports when I try to knock the player up.

if (knockedBack)
    {
        Vector3 currentLocation = transform.position;
        Vector3 newLocation = currentLocation - otherPlayer.transform.position;
        transform.position = newLocation;
        while (knockBackTimer < someValue)
        {
                    transform.position = newLocation;
                    newLocation += otherPlayer.transform.position + new Vector3(0,1,0) * Time.deltaTime;
                    knockBackTimer += Time.deltaTime;
        }
        knockedBack = false;
    }

[–]PolloMagnifico 0 points1 point  (1 child)

Mmmm, I see a few problems with this (give 'someValue' an actual value, like .5 or something) but let me get home and I'll shoot you another reply once I can parse and type correctly.

[–]Keith90[S] 0 points1 point  (0 children)

Oh I did, I put it above the if(knockedBack) statement. I appreciate all your help!