#Handling Moving Platforms with my player.
1 messages · Page 1 of 1 (latest)
if (hit.rigidbody != null)
{
var TargetVel = hit.rigidbody.GetPointVelocity(hit.point);
rb.linearVelocity += TargetVel - LastPlatVel;
LastPlatVel = TargetVel;
{
📃 Large Code Blocks
Use links to services like:
https://paste.mod.gg/, https://hastebin.skyra.pw/, https://paste.ofcode.org/, https://paste.myst.rs/
📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
like what?
you've been showing detached sections of code
we need context on how the code flows
this is all the player code that handles the platform stuff, as i said before i am using a floating rigidbody.
where do you apply velocities, what leads up to that, what message is the code in
it's right there in the code? i apply the velocity straight to the rigidbody.
the hit is a raycast. sorry i didn't mention that.
just send the whole method or the whole script if you can't figure out which parts are relevant
i'd rather not if you don't mind, and the only piece of code that connects to this is the raycast code(literally just a Physics.Raycast), also the method is inside FixedUpdate.
ok so the answer to this
the moment the player gets on - is the platform's existing velocity applied?
is that baked into the delta check?
was apparently that it's baked into the delta check
if you aren't doing any collision messages, i guess
yes.
what do you mean actual behaviour? i've shown the code right right above.
a description of what happens to the player on the platform
a video, perhaps
what's the actual issue currently
a raycast is shot down from the player, it then checks the velocity of the platform at that point using GetPointVelocity(hit.point) then applies that velocity directly(this is changing alot as i'm trying to find the best method to apply the velocity to stop the issues i'm having). p.s if that doesn't help i'll send a video.
yeah that really isn't what i asked at all lmao
wasn't that a "a description of what happens to the player on the platform"?
nope, that's what the code does
that's not what actually happens to the player
i'm asking, what is the issue
ohhhh... sorry.
what happens is that any time the platform experiences a large velocity change the velocity applied tends to spike unnaturally.
so is the player actually tracking the platform through small velocity changes then?
yes!
bro that's the thing you were complaining about earlier 💀
you gotta get better at providing info
sorry... i tend to do that.
it took a solid 40 minutes just to figure out that this #💻┃code-beginner message wasn't actually an issue
so what's actually happening with the large velocity changes
just.. send a video
sooo it wasn't tracking through small velocity changes at all?
what do you mean?
would also help if you removed the search and let the logs scroll so we could actually see the difference in velocity each tick
also perhaps debug the player velocity in comparison to the platform velocity
also check if the player has damping or anything like that
the player doesn't have any damping also those log where using outdated data.
exactly why i said to let the logs scroll
disable your other irrelevant logs for now
keep the previous log of the delta values, and log this velocity and the player velocity
what delta values?
also, just to make sure - you aren't moving when you're on the platform, right
#💻┃code-beginner message this one
i'm applying the velocity like this rb.linearVelocity += PlatVel;
whyy
ok tell me what was wrong with this
stop making random changes
the values are way too small like .01 small
were they actually not moving the player or did you just think they were too small
.01 of acceleration per tick is normal, that's 0.5 of acceleration per second
well it's seemingly alot slower than that.
i am applying that velocity to the player.
show that
this TargetVel - LastPlatVel.
Platform Basics
- Detecting a platform: Check if the player is standing on one (e.g., raycast or collision).
- Getting platform velocity: Ask the platform how fast it’s moving (GetVelocity() method).
- Adding platform velocity: Combine the platform’s movement with the player’s own movement.
- Leaving the platform: Stop adding the platform’s velocity when the player steps off.
Example Psuedo Code
Vector3 velocity = playerInput;
if (onPlatform)
{
velocity += platform.GetVelocity();
}
rb.velocity = velocity;
- playerInput = your normal movement
- platform.GetVelocity() = the platform’s motion
- combine them so the player moves with the platform
Tips:
- Frame timing matters: always use
FixedUpdate()for physics. so platform and player velocities stay in sync. - Floating/ non-colliding players: if ur player doesn't physically touch the platform use raycasting or check distance
- Vertical motion: if the platform moves up and down you need to apply its vertical velocity too or the player will appear to float in place or fall through
- Smoothing: platforms can jitter the player if velocity is applied raw.. Lerp or smoothing can help, especially for small or fast moving platforms
- Leaving the platform; Always clear the platform reference and the velocity you were adding (this keeps the player from keeping any of the residual velocities)
- Scaling and Rotation: If a platform rotates or scales, its velocity may need to be converted to world-space
thats about all I got.. ( i can show some of the code i used [in this test](#1428033349997297725 message) but its basically summed up by [this example code block](#1428033349997297725 message)
Good luck,
SpawnCamp† — your friendly Unity prototyper 🕹️
this wouldn't really work with non-velocity-setting forms of moving though, would it
you mean like if they were using AddForce instead?
yeah
ohh crud
yea for that first thing comes to mind is temporarily attaching it as a child..
and overhauling the movement code to work in local spaces
oh yea, i hadn't even thought about that.. i just recalled he was using a rigidbody.. and I was also using a rigidbody for my current prototype...
went through all that work to implement platforms and i don't even need em 😬
does that work properly with rigidbodies? i feel like i recall there being issues with that but i don't really remember
anyway, even if it is the wrong type of movement (i directly control my velocities...) here's a revised version of the code i have in that video..: https://paste.myst.rs/w2q2l8la
figure'd i'd just go ahead and paste it anyway since i wrote it out
-# 💡 there still may potentially be something useful in there for ya
a powerful website for storing and sharing text and code snippets. completely free and open source.
only issue's i remember having with it was jumping (in and out of a trigger [you'd need to make a more robust raycasting detection type deal]) and if/or platform was tilting.. that req yet more robustness
i have spring forces.. soo thats why there seems to be a bit of lag in directional changes in the example video up there ☝️
but if i disable the springs it works pretty reliably
❕ i keep the platforms velocity in a totally different cached variable
so i can nuke it off the velocity when exiting the platform
that was the only issue i had in that implementation ^ but idk if its foolproof**
though there is a concern i have - without SEO/latefixedupdate, wouldn't this make the player lag behind the platform by a tick
probably not gonna be enough to be relevant, but hey, it's there (i think)
oh.. perhaps.. 🤔 i mean..
to me its negligible.. but it may
i'd have to do some debugging w/ ticks and stuff to know for sure
i dont have any jitterying or anything also b/c i was using a spring-based groundcheck
player doesn't even technically touch the ground... soo the platform never gets a chance to clip or anything like that
hmm, this is interesting.. never thought about it
your more advanced than I ofc.. and have better foresight and can anticipate things like that 😅
i don't have a rb that uses forces tho 🙁
i would imagine u could just do the same concept.. but before the addforce call..
it might happen before the simulation tick, where i think accumulated forces are turned into velocity?
my current solution rn is to use rb.MovePosition but that's jittery and doesn't really have the physically that i want.
rb.MovePosition(TargetPos);```
For moving platforms, I reparent to the platform then move players local position/rotation
yeah i tried that but wouldn't that remove all physicality?
You could handle the physics manually. Or you could use physics joints to attach to the platform
that seems like a really janky way of doing it? (also i've tried using physics joints)
I havent had any issues with reparenting. If the player jumps or collides with anything you can immediately unparent
hey i'm having issues with parenting the rigidbody as its a little jittery.
try making the rigidbody kinematic
that wouldn't really work in my scenario.
u figured this out yet beggam?
turns out my implementation may have some lag during directional changes...
i left my player standing on top of it for about 3 hours and it never fell off.. but it does seem to lag behind when it changes directions..
(i contributed it to my spring setup on my player but i actually don't believe thats it)
actually.. funnily enough i seem to have gottten rid of it by tweaking the spring's dampening forces..
(red being the one thats just taking the platforms vel and adding it to itself)
(green is just a normal controller thats ignoring the platform forces)
its not perfect.. but for basic elevators and things i think it'd be just fine.. (especially once the camera smoothing is implemented for the player..)
i doub't u'd notice it
I didnt read all the comments, but there is a moving platform in this one maybe check that
i was hunting for something in the learn section for quite a while yesterday..
glad u found something.
its hard to search the specific context of those tutorials..