#Making MooMoo.io alike movement physics
1 messages · Page 1 of 1 (latest)
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. 😅
no I only kept one collider on tree
then you would check if the trigger touches another RB then if it does slow it down until 0 by using its velocity
and added the big collider for that movement to player
so like loop
ah then remove the trigger on your player and put it on the tree, then just do as i said here #1288216306348326983 message
no, you would slow it down by doing the opposing velocity
although it probably uses overlap2d.circle on the player but a trigger would suffice for now
use ontriggerstay2d
just to make sure if this is what u tellin me\
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
anything will do as long as it creates the same kind of illusion
idk what I wrote but would this do it ? haha
dude i tried joints and it wont give u 2-3 vibrations 😦
i think u should just animate it
def not
idk this seems more of a solution to me than animating
animating confuses me kind
kinda
coz I want this to be more of a physics thing than just animation
hmm yeah
you would also put ontriggerstay2d on the tree btw
so a new script
and here is the documentation for it https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerStay.html
thankk u
no need to thanks bro
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
you could either add a force opposite of its velocity to slow it down or use its velocity directly to slow it down
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
another joint thing xdd
i tried it
why are you trying to give him wrong information?
wth dis
trying out won't hurt anyway right
but if u say
this won't work
noo i tried it just now
i'll continue w that
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
let me show u
Slowing down the velocity will also make you kind of stick to the tree when moving away which I dont think is good
oh yeah
i think
then use addforce
pushing out will work
I would just add a force away from the tree
I just don't think like
see
How to get the position player is coming from
just 3 steps and done
thats not what he wants
u can control the damping by fixing frequency
you can clearly see that the tree is not using joints
lmao yeah i forgot he is not using gravity
but instead applying a force opposite of the RB velocity
but can still work if he add force on the tree by the player
Yeah
to the player
but like
To add force I should like tell
Where should I add force to right
opposite to the RB velocity
ohh my bad i thought he just want the shakiness of the tree to happen
collision.attachedRigidbody.AddForce(-collision.attachedRigidbody.Velocity * Multiplier, Forcemode.force);
oh I could've just done -
now I feel so stupid
then check if velocity is 0 or near 0 and stop applying the force
- Multiplier won't work
hmm but i will still tell u to use spring joint
lol anyways good luck guys :3
because then shouldn't multiplier be like vector2
ty
no, its just a float
i would !learn how to use unity
:teacher: Unity Learn ↗
Over 750 hours of free live and on-demand learning content for all levels of experience!
oh yeah mb I fixed I had some other error
And that would pull you towards the tree when walking away from it
yeah i see that, im thinking of a solution
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
thats most likely because your multiplier is 0
{
collision.attachedRigidbody.AddForce(-collision.attachedRigidbody.velocity * multiplier, ForceMode2D.Force);
}```
its 5
How are you moving the character in the first place?
Are you using rigidbody.velocity?
If you directly set velocity then it will override any addforce calls
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;```
addforce basically adds to the velocity
rb.addforce, but it's a bit more complex than that
makes sense
Just using addforce will make it pretty sluggish
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
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
what that someMultiplier would be
any float you desire
but I would do what he said with the velocityDiff so you dont get pulled back to the tree
i like this movement a lot
more
but I do get pulled back now and I noticed that well too
Is this just addforce(movedir) or what I suggested?
im guessing you are still using #1288216306348326983 message
did it solved??
not yet
in the tree script
Replace the speed inside addforce with a different float. That float controls the "snappiness" of the movement
Like how fast you accelerate/decelerate
I see
you would use #1288216306348326983 message
what osmal said in the tree script
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
Vector2 velocityDiff = targetVelocity - rb.velocity;
rb.AddForce(velocityDiff * acceleration);```
I see
how would I implement
that
That looks ok, now you just need to tweak acceleration to your liking
Yeah I did that too
But the "pushing away from trees" is still not there
well exactly how you added what i showed you
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");
}```
but instead of using -rb.velocity you use his method
ok wait
Something like this would work?
no
Im still getting pulled wait i'll try to fix
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
wym by this part
//character position - transform.position //tree position
Vector2 diff = collision.transform.position - gameObject.transform.position
like subtract them?
//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
Ik now that
im good w c#
not good w unity library
There's a ;, the comments just make it look like there isnt
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
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
wait, you are not using diff you are using velocitydiff
so of course it wouldnt work
I'm talking about diff in the code that we can see in the video
i used position thing too the one u wrote
Oh yeah you changed it to velocityDiff? Use diff
fixed that and now it doesn't move at all
ok ok
wait
Yeah good idea to use a larger trigger collider for debug purposes
yeah
so here
but if u noticed guys
Seems to work?
Just need a larger force away from the tree
Can't comment on anything if I don't see your current code
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
I don't think that's necessary
Yes, if you stop completely while inside the trigger then that prevents it from pushing you out
logically
if I stop yeah
it won't add any more force to me
which is not what we want
You might want to add some modifiers according to your current movement speed later but get this working first
what modifiers
well if you remove this it will keep adding a force even if you are stopped so it will eventually push you out
also you can just do vector2.zero
Let's see how it ends up feeling first
what u think guys
oh thats nice thing will be useful in future too
The functionality is there but I feel like the push-away force should be greater
yes
So try a greater value than 45
i keep adding it xdd
yeah
this was actually 80
and I changed it to multiplier
so I can change it from unity
try 200
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
since it turns out smooth
its fine
well there you go 👍
Looks good
Np, was fun
just started solo learning on unity and i'll keep asking questions here from now on xdd
feel free to ask
finally we all did it guys was nice to work with u all 🙂
u can delete it
right click on it, and delete thread
yeah ok
You don't need to delete it, just in case someone has a similiar issue later
We can reference this
yeah
lmao it got deleted
I guess it's gone after we leave then?
i mean no other person can see other than us i guess
check physics
It's fine, I'll remember how it was done
xd