#Making MooMoo.io alike movement physics

1 messages · Page 1 of 1 (latest)

toxic tangle
#

alright

pale tartan
#

Oh I can my bad

split magnet
#

you would use the ontriggerstay2d, and im not to sure but the trigger is either on the player or the tree. its probably on the tree so if you still have the 2 colliders on the the tree then use that. 😅

pale tartan
#

no I only kept one collider on tree

split magnet
#

then you would check if the trigger touches another RB then if it does slow it down until 0 by using its velocity

pale tartan
#

and added the big collider for that movement to player

split magnet
split magnet
pale tartan
#

now in playermovement script

#

How would I detect if rb touched that

split magnet
#

although it probably uses overlap2d.circle on the player but a trigger would suffice for now

split magnet
pale tartan
#

just to make sure if this is what u tellin me\

celest sandal
#

In moomoo the "triggers" actually push you away from them instead of just slowing you down

#

But if you don't need an exact replica then this will probably do

pale tartan
#

anything will do as long as it creates the same kind of illusion

#

idk what I wrote but would this do it ? haha

toxic tangle
#

dude i tried joints and it wont give u 2-3 vibrations 😦

#

i think u should just animate it

pale tartan
pale tartan
#

animating confuses me kind

#

kinda

#

coz I want this to be more of a physics thing than just animation

toxic tangle
#

hmm yeah

split magnet
#

so a new script

pale tartan
#

oh i got it

#

just made sure if it detects and yes

split magnet
pale tartan
toxic tangle
split magnet
# pale tartan thankk u

well im guessing you have the ontriggerstay2d script on the tree so now you just need to use its collider's rigidbody's velocity and slow it down

pale tartan
#

how would I like

#

slow it down

split magnet
#

you could either add a force opposite of its velocity to slow it down or use its velocity directly to slow it down

toxic tangle
#

dude good news

#

i got how u can do it

#

that too very easy

#

add a spring joint and this will work perfectly

#

give it a try

pale tartan
#

another joint thing xdd

toxic tangle
#

i tried it

pale tartan
#

ok

#

i'll add to other tree

toxic tangle
#

its very easy

#

and it will work

split magnet
#

why are you trying to give him wrong information?

pale tartan
#

wth dis

pale tartan
#

but if u say

#

this won't work

toxic tangle
pale tartan
#

i'll continue w that

split magnet
#

a spring joint will make the player not move, as it is a spring

#

it will act like a spring but keep it locked at a certain point

#

joints will not work in this case

#

so quit it with joints

toxic tangle
#

let me show u

celest sandal
#

Slowing down the velocity will also make you kind of stick to the tree when moving away which I dont think is good

split magnet
#

then use addforce

pale tartan
#

pushing out will work

celest sandal
#

I would just add a force away from the tree

pale tartan
#

I just don't think like

pale tartan
#

How to get the position player is coming from

toxic tangle
#

just 3 steps and done

pale tartan
#

like if I add addforce right

#

it takes vector

split magnet
toxic tangle
#

u can control the damping by fixing frequency

split magnet
#

you can clearly see that the tree is not using joints

toxic tangle
#

lmao yeah i forgot he is not using gravity

split magnet
#

but instead applying a force opposite of the RB velocity

toxic tangle
#

but can still work if he add force on the tree by the player

split magnet
#

you dont add a force on the tree

#

only to the player

pale tartan
#

to the player

#

but like

#

To add force I should like tell

#

Where should I add force to right

split magnet
#

opposite to the RB velocity

pale tartan
#

realizing c# skills won't work in unity xd

toxic tangle
#

ohh my bad i thought he just want the shakiness of the tree to happen

split magnet
#

collision.attachedRigidbody.AddForce(-collision.attachedRigidbody.Velocity * Multiplier, Forcemode.force);

pale tartan
#

I did this

pale tartan
#

now I feel so stupid

split magnet
#

then check if velocity is 0 or near 0 and stop applying the force

pale tartan
#
  • Multiplier won't work
toxic tangle
#

hmm but i will still tell u to use spring joint XD_lmao lol anyways good luck guys :3

pale tartan
#

because then shouldn't multiplier be like vector2

split magnet
#

i would !learn how to use unity

limpid widgetBOT
#

:teacher: Unity Learn ↗

Over 750 hours of free live and on-demand learning content for all levels of experience!

pale tartan
#

oh yeah mb I fixed I had some other error

celest sandal
split magnet
#

yeah i see that, im thinking of a solution

pale tartan
#

don't really work

#

and it doesn't pull me to tree either

celest sandal
#

What I'd do is something like this (pseudocode) cs vec2 diff = characterPosition - treePosition addforce(-diff.normalized * someMultiplier)

#

diff is a vector pointing from tree to you

split magnet
pale tartan
#
{
    collision.attachedRigidbody.AddForce(-collision.attachedRigidbody.velocity * multiplier, ForceMode2D.Force);
}```
celest sandal
#

How are you moving the character in the first place?

#

Are you using rigidbody.velocity?

pale tartan
#

yes

#

I use velocity

celest sandal
#

If you directly set velocity then it will override any addforce calls

pale tartan
#
float moveX = 0f;
float moveY = 0f;

//Player Rotate Lock.---------------------------------------------------------
if (Input.GetKey(KeyCode.X)) { if (PlayerRotates) PlayerRotates = false; else PlayerRotates = true; }
//Player Rotate Follows Mouse.---------------------------------------------------------------
if (PlayerRotates)
{
    Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
}

//Sprinting.--------------------------------------------------------------
if (Input.GetKey(KeyCode.LeftShift) && stamina >= 2.2f && rb.velocity.magnitude > 0)
{
    //getting camera animation----
    animator.SetTrigger("Running");
    //speed becomes boostedSpeed----
    speed = speedBoost;
    //reducing stamina on cost of runcost---
    stamina -= runCost * Time.deltaTime;
    if (stamina < 0) stamina = 0;
    staminaBar.fillAmount = stamina / maxStamina;

    //new Color(234, 244, 84) - normal color.
    //run - new Color(187, 194, 79) - sprint color.

    staminaBar.color = new Color(187f / 255, 194f / 255, 79f / 255);
    //reseting Coroutine----
    if (recharge != null) StopCoroutine(recharge);
    recharge = StartCoroutine(RechargeStamina());
}
else
{
    animator.SetTrigger("EndRunning");
    speed = 30f;
}
//Movement On Keys.------------------------------
if (Input.GetKey(KeyCode.W)) moveY = +1f;
if (Input.GetKey(KeyCode.S)) moveY = -1f;
if (Input.GetKey(KeyCode.A)) moveX = -1f;
if (Input.GetKey(KeyCode.D)) moveX = +1f;
//PlayerMovement.----------------------------------------
Vector3 moveDir = new Vector3(moveX, moveY, 0f).normalized;
rb.velocity = moveDir * speed;```
celest sandal
#

addforce basically adds to the velocity

pale tartan
#

ohh

#

rb.velocity.addforce

#

and movedir*speed right

celest sandal
#

rb.addforce, but it's a bit more complex than that

pale tartan
#

makes sense

celest sandal
#

Just using addforce will make it pretty sluggish

split magnet
#

i mean if he wants to make it as close to the game its best to use addforce

#

you can feel it in the game

pale tartan
#

yeahh xdd

#

I defenitely doo

celest sandal
#

Just saying, there's a neat formula that I use for addforce:cs vec2 targetVelocity = // your movedirection * speed vec2 velocityDiff = targetVelocity - rb.velocity rb.addforce(velocityDiff * someMultiplier)

#

But sure you can try just adding the movedirection as force. See how it goes

pale tartan
#

what that someMultiplier would be

split magnet
#

any float you desire

#

but I would do what he said with the velocityDiff so you dont get pulled back to the tree

pale tartan
#

i like this movement a lot

#

more

#

but I do get pulled back now and I noticed that well too

celest sandal
split magnet
toxic tangle
#

did it solved??

split magnet
#

not yet

pale tartan
#

this

#

oh

celest sandal
# pale tartan

Replace the speed inside addforce with a different float. That float controls the "snappiness" of the movement

pale tartan
celest sandal
#

Like how fast you accelerate/decelerate

pale tartan
#

I see

split magnet
#

what osmal said in the tree script

celest sandal
#

The idea of the movement code I suggested is that if you are already moving at the target velocity, it won't add any force. This way you won't overshoot

pale tartan
#
Vector2 velocityDiff = targetVelocity - rb.velocity;
rb.AddForce(velocityDiff * acceleration);```
celest sandal
pale tartan
#

Yeah I did that too

celest sandal
#

But the "pushing away from trees" is still not there

split magnet
pale tartan
#
private void OnTriggerStay2D(Collider2D collision)
{

    if (collision.attachedRigidbody.velocity != new Vector2(0, 0))
    {
        collision.attachedRigidbody.AddForce(-collision.attachedRigidbody.velocity * multiplier, ForceMode2D.Force);
    }
    Debug.Log("push out");
}```
split magnet
#

but instead of using -rb.velocity you use his method

pale tartan
#

ok wait

#

Something like this would work?

#

no

#

Im still getting pulled wait i'll try to fix

split magnet
#

so most likely

Vector2 diff = collision.transform.position //character position - transform.position //tree position
collision.attachedRigidbody.addforce(-diff.normalized * someMultiplier)
#

basically what diff does is make a new vector by comparing the character position by tree position

pale tartan
#

wym by this part
//character position - transform.position //tree position

#

Vector2 diff = collision.transform.position - gameObject.transform.position

#

like subtract them?

split magnet
#

yes and makes a vector from that

#

all // this is, is a comment

pale tartan
#
//Vector2 velocityDiff = targetVelocity - collision.attachedRigidbody.velocity;


Vector2 diff = collision.transform.position - gameObject.transform.position; //character position - transform.position //tree position


if (collision.attachedRigidbody.velocity != new Vector2(0, 0))
{
    //collision.attachedRigidbody.AddForce(velocityDiff, ForceMode2D.Force);
    collision.attachedRigidbody.AddForce(-diff.normalized * 45);
}
Debug.Log("push out");```
#

yeah and idk if it works

#

it doesn't seem to slow up

pale tartan
#

im good w c#

#

not good w unity library

split magnet
#

you should be getting an error

#

since you do not have a ;

#

at the end of diff

pale tartan
#

I do have

#

im not getting error

celest sandal
#

There's a ;, the comments just make it look like there isnt

pale tartan
#

yeah

#

I wrote this script now

 Vector2 velocityDiff = gameObject.GetComponent<Rigidbody2D>().velocity - collision.attachedRigidbody.velocity;


 Vector2 diff = collision.transform.position - gameObject.transform.position; //character position - transform.position //tree position
 

 if (collision.attachedRigidbody.velocity != new Vector2(0, 0))
 {
     //collision.attachedRigidbody.AddForce(velocityDiff, ForceMode2D.Force);
     collision.attachedRigidbody.AddForce(-velocityDiff.normalized * 45, ForceMode2D.Force);
 }
 Debug.Log("push out");

#

and this is how it behaves wait

#

so when I stop it keeps moving by itself

#

I'll actually make collider a bit bigger so its easier to notice

celest sandal
#

First off, you shouldn't invert diff

#

Not -diff, just diff

#

It's a vector pointing from the tree to the player. So it should push the player away from the tree

split magnet
#

wait, you are not using diff you are using velocitydiff

#

so of course it wouldnt work

celest sandal
#

I'm talking about diff in the code that we can see in the video

pale tartan
celest sandal
#

Oh yeah you changed it to velocityDiff? Use diff

pale tartan
#

ok ok

#

wait

celest sandal
#

Show your current code and stop changing it for a moment

#

So we can keep up

pale tartan
#

I think we worked it out

#

a bit

#

when a collider is large u can easily see it wait

celest sandal
#

Yeah good idea to use a larger trigger collider for debug purposes

pale tartan
celest sandal
#

Seems to work?

pale tartan
#

in the last case

#

when I moved inside

celest sandal
#

Just need a larger force away from the tree

pale tartan
#

I actually stopped

#

moving

#

and it didn't push me out

#

i'll try changing some

celest sandal
#

Can't comment on anything if I don't see your current code

pale tartan
#

values to multipliers

#
private void OnTriggerStay2D(Collider2D collision)
{

    //Vector2 targetVelocity = -collision.attachedRigidbody.velocity * multiplier; // your movedirection * speed
    //Vector2 velocityDiff = gameObject.GetComponent<Rigidbody2D>().velocity - collision.attachedRigidbody.velocity;


    Vector2 diff = collision.transform.position - gameObject.transform.position; //character position - transform.position //tree position
    

    if (collision.attachedRigidbody.velocity != new Vector2(0, 0))
    {
        //collision.attachedRigidbody.AddForce(velocityDiff, ForceMode2D.Force);
        collision.attachedRigidbody.AddForce(diff.normalized * 45, ForceMode2D.Force);
    }
    Debug.Log("push out");
}```
#

like it has a weak spot here xd

#

from the other sides it pushes me out

#

it might be because of this

if (collision.attachedRigidbody.velocity != new Vector2(0, 0))
#

wyt

celest sandal
pale tartan
#

yeah I think that makes it stop

#

wait i'll remove that

celest sandal
#

Yes, if you stop completely while inside the trigger then that prevents it from pushing you out

pale tartan
#

logically

#

if I stop yeah

#

it won't add any more force to me

#

which is not what we want

celest sandal
#

You might want to add some modifiers according to your current movement speed later but get this working first

pale tartan
#

what modifiers

split magnet
#

also you can just do vector2.zero

celest sandal
pale tartan
pale tartan
celest sandal
pale tartan
#

yes

celest sandal
#

So try a greater value than 45

pale tartan
#

i keep adding it xdd

pale tartan
#

and I changed it to multiplier

#

so I can change it from unity

toxic tangle
#

try 200

celest sandal
#

The reason why it has to be a high number is because it's fighting your other movement code's addforce

#

But that's fine I think

pale tartan
#

its fine

split magnet
#

well there you go 👍

pale tartan
#

look guys

#

put 250 and made outside collider a bit more smaller

celest sandal
#

Looks good

pale tartan
#

Good way more smooth

#

thank u guys! <3

celest sandal
#

Np, was fun

pale tartan
#

just started solo learning on unity and i'll keep asking questions here from now on xdd

split magnet
#

feel free to ask

toxic tangle
#

finally we all did it guys was nice to work with u all 🙂

pale tartan
#

haha

#

thank yall

#

how do I close thread

#

or should it be saved or what

toxic tangle
#

u can delete it

split magnet
#

right click on it, and delete thread

pale tartan
#

yeah ok

celest sandal
#

You don't need to delete it, just in case someone has a similiar issue later

#

We can reference this

toxic tangle
#

yeah

pale tartan
#

yeah ok

#

i thought I deleted but

#

it stayed so lets leave it like this ig

toxic tangle
#

lmao it got deleted

celest sandal
#

I guess it's gone after we leave then?

toxic tangle
#

i mean no other person can see other than us i guess

pale tartan
#

check physics

celest sandal
#

It's fine, I'll remember how it was done

pale tartan
#

xd