Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411421 Posts in 69363 Topics- by 58416 Members - Latest Member: timothy feriandy

April 18, 2024, 02:42:41 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Trying to implement easing Math on player controlled object
Pages: [1]
Print
Author Topic: Trying to implement easing Math on player controlled object  (Read 749 times)
jabcog
Level 0
**


View Profile
« on: June 20, 2018, 07:34:35 PM »

I've had a look around tigsource and the internet in general and haven't found much in the way of help so hopefully this isn't doubling up on a discussion somewhere.

Recently I watched juice it or lose it and have been trying to implement easing formula in C# and Unity and it's is really doing my head in. Hopefully someone here can help.

I've found a couple of sources for formula already formatted for C# but the one that makes the most sense is this one: https://gist.github.com/xanathar/735e17ac129a72a277ee

and here's my code

Code:
void Update () {
        intermediaryPos = gameObject.transform.localPosition;

        if (time < duration)
        {
            time += Time.deltaTime * timeMultiplier;
            gameObject.transform.localPosition = new Vector3(Easing.CircEaseInOut(time, currentPosX, destinationPosX, duration), transform.position.y, transform.position.z);
        }

        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            currentPosX = intermediaryPos.x;
            destinationPosX = destinationPosX - speed;
            time = 0f;
        }

        else if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            currentPosX = intermediaryPos.x;
            destinationPosX = destinationPosX + speed;
            time = 0f;            
        }
    }

I think I understand what it's doing but I've been really struggling to implement it without any erroneous behavior. Basically what is happening is when i press left or right arrows once, and allow the object to reach its destination then press the opposite direction nothing happens till i press it a second time. So in this case the formula doesn't move the value from whatever back to 0.

Also what happens when i press the same direction more than once is that the second time the object will move twice as far as it did the first time.

Is anyone here able to point me in the right direction?
Logged
Ordnas
Level 10
*****



View Profile WWW
« Reply #1 on: June 21, 2018, 12:24:02 AM »

I can't understand very much what you want to achieve, but looking at your code: is it correct that you are using CurrentPosX inside the Easing, when CurrentPosX will be overriden everytime you push a button?
Logged

Games:

whistlerat
Level 1
*



View Profile
« Reply #2 on: June 21, 2018, 03:01:29 AM »

What's your intended behaviour? You want it to ease to a new location on pressing a direction button once? Do you want it to be able to have its movement interrupted and get a new destination, or should it just cleanly go from position to position?

I grabbed the easing functions and your code and had a quick play and something weird is definitely going on. To better visualise what your code is doing, try adding this to your script and looking in the editor scene view:

Code:
    void OnDrawGizmos()
    {
        Gizmos.color = Color.red;

        Gizmos.DrawSphere(new Vector3(destinationPosX, transform.position.y, transform.position.z), 0.1f);

        Gizmos.color = Color.blue;

        Gizmos.DrawSphere(new Vector3(currentPosX, transform.position.y, transform.position.z), 0.1f);
    }

From my initial tests, the destination and current/origin positions are generated correctly, but the actual movement of the object doesn't then go to the destination.
Logged

ferreiradaselva
Level 3
***



View Profile
« Reply #3 on: June 21, 2018, 04:07:05 AM »

I can't understand very much what you want to achieve, but looking at your code: is it correct that you are using CurrentPosX inside the Easing, when CurrentPosX will be overriden everytime you push a button?
^^^

You don't use easing functions for controlling an object. Easing functions are useful for animations - where the beginning is always the same, and the end is always the same.

If you want to smooth the motion, use acceleration instead of applying a fixed velocity.
Logged

whistlerat
Level 1
*



View Profile
« Reply #4 on: June 21, 2018, 06:18:24 AM »

I can't understand very much what you want to achieve, but looking at your code: is it correct that you are using CurrentPosX inside the Easing, when CurrentPosX will be overriden everytime you push a button?
^^^

You don't use easing functions for controlling an object. Easing functions are useful for animations - where the beginning is always the same, and the end is always the same.

If you want to smooth the motion, use acceleration instead of applying a fixed velocity.

Yeah, using acceleration/dampening will make a lot more sense. The easing is only really useful if you're moving in very fixed ways, but you probably do want to avoid it for this anyway.
Logged

jabcog
Level 0
**


View Profile
« Reply #5 on: June 21, 2018, 08:50:16 PM »

Thanks for the input everyone.

What's your intended behaviour?

I was hoping that i could use the easing formula to add character to player movement - and that by changing the origin and destination in realtime the formula would just adapt. So for example in a breakout or pong, instead of having the paddles move just on player input I thought that I'd be able to add a little bit of bounce or ease in/out after player input stops.

It may be that to use these formula they need to be isolated from changing variables(current and intended poitions) but that would cancel the opportunity for ease-in using them. I haven't done any further testing since yesterday though.

Maybe if time and duration are simply used to calculate a percentage to place your moving object on the curve, then you need to subtract from time every frame you move the destination? I'm just thinking aloud though.

Considering how much time I've already spent on working this out I may just bite the bullet and use a tweening engine or animation curves but I really wanted to try to use the formula. I'd be super greatful for any kind of advice or thoughts
Logged
jabcog
Level 0
**


View Profile
« Reply #6 on: June 21, 2018, 08:56:58 PM »

I can't understand very much what you want to achieve, but looking at your code: is it correct that you are using CurrentPosX inside the Easing, when CurrentPosX will be overriden everytime you push a button?
^^^

You don't use easing functions for controlling an object. Easing functions are useful for animations - where the beginning is always the same, and the end is always the same.

If you want to smooth the motion, use acceleration instead of applying a fixed velocity.

Ahh gotcha, so things like elements on a webpage or ui elements as opposed to more dynamically moving objects?

This is the talk that I've been looking at to give a better idea of the kinds of things I'd like to be able to do:

Logged
sundance
Level 0
*


View Profile
« Reply #7 on: June 25, 2018, 11:04:51 AM »

Yeah I would second whistlerat here. I think what you are looking for in this case is dampening. Tweening will rely on you knowing the start position and the stop position of whatever object you are interested in moving. This is fine for a tile based game where you move between distinct locations but not so great if you are sliding around on an open plain. So I think it would be best if you (simply upon player release of key) multiply your velocity by some dampening/friction coefficient between 0-1. As you apply the coefficient per frame you'll approach an asymptote so you'll need to create a cutoff where the object comes to a complete rest. The coefficient could even come from whatever surface the object is colliding with so you can have different interactions between surfaces.

If you are still trying to pursue the tweening you will likely continue to get the effect you describe where the player cannot adjust the movement of the object until the tween is fully executed in order to minimize this effect you could (I suppose) make many micro tweens but i'm not sure how you ultimately create the desired effect from that.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic