- Home /
remove forward component from velocity vector
Got a model with a rigidbody, a move script and a basic raycast.
When the raycast comes into contact with an obstacle, i want the model to stop "forward" (the unit's nose direction) velocity only. I want all other directional velocities to be unaffected.
when i say forward i do mean the objects own 'forward'. if the model is falling or moving diagonally, it's still only the forward i want to affect, not any other movement
JUST stop velocity in the direction of the nose of the object.
Halp?
Tom :)
$$anonymous$$issed that out of my question, sorry:
Yeah, when i say forward i do mean the objects own 'forward'.
And what i mean by 'keep all other velocities, is that ONLY the forward is reduced to zero, if the model is falling or moving diagonally, it's still only the forward i want to affect, not any other movement.
Hope that cleared it up!
$$anonymous$$ :)
I've never used Vector3Project before, so i can't figure this last bit out (BUT I FEEL SO CLOSE!):
Error: $$anonymous$$ identifier: 'r'
Answer by Fattie · Oct 08, 2012 at 07:31 PM
so you need to have good primary school Vector math here ! :)
you have your velocity V.
you need the FORWARD COMPONENT of V.
by "forward" i mean the way your guy is pointing -- or indeed, any direction you are interested in
to get that use this.
Documentation/ScriptReference/Vector3.Project.html
if you don't know what "projecting a vector" means simply google it for 1000s of pages of diagrams and explanations
so that will give you the Forward Component of velocity, FC
next simply take V and subtract FC
that will be your new velocity. Set it !
or the no bullshit version ..
rigidbody.velocity
= rigidbody.velocity - Vector3.Project(
rigidbody.velocity, transform.forward )
amazingly unity makes it that simple.
don't forget if it's moving perfectly straight ahead this will of course stop it.
And thanks for your input and time, $$anonymous$$uuskii :)
I put the Vector3.Project into a simple raycast script, works well :)
"What i'm trying to do is have my player be able to jump up the sides of walls."
Okay if you say he's dealing with an aircraft whatever. I'm just tired of your sarcastic and obnoxious attitude on here. $$anonymous$$aybe that's cool with other people but it's repulsive to myself.
Raycasts still have the chance of "slipping" past things so using them as colliders is still not too great of an idea.
Answer by Muuskii · Oct 08, 2012 at 07:23 PM
Is this supposed to be some sort of collision solution? Because these sort of methods don't tend to work out very well.
Why does it have to be a raycast? Does it really have to have an infinite range? If not why not use a collider? Same thing about why does it have to be a line? If not, a collider will be better as well.
If it needs to be infinite, and is more like a "beam that pushes us away from things" why not use a force on a rigidbody?
It's actually collision of any kind i'm trying to avoid. The way 2 colliders interact messes up other elements of my game.
It would work much better for me if i can cease the objects forward velocity only, when it reaches a certain distance from an obstacle.
This is an answer because I am providing the solution of using a COLLIDER, or forces. I am asking rhetorical questions, not asking for additional information.
Your description is correct:
JUST stop velocity in the direction of the nose of the object.
WOAH! "The way 2 colliders interact messes up other elements of my game." that seems like a code smell. if that's happening you might need to rethink the way you've designed your game.
If you're looking to make some sort of "leash" to make sure that players don't leave the arena, most games use invisible walls. Like colliders attached to empty objects.
But if something in your game gets messed up when colliders interact then it seems to be causing you more trouble than it is worth! Try to find a different way of making those elements without getting messed up with colliders.
@Fattie, a raycast is still not a good solution for this as you can "miss" a building's wall very easily. You can still use colliders with a $$anonymous$$inematic Rigidbody, the collision contact points let you know when you've hit a building. After a collision stop the forward velocity and use a standard timer for the jump.
The collision physics won't interfere with your other game elements and you won't have raycasts "slipping into cracks"
Raycasts are not really a good substitute for normal old collisions.
Your answer
Follow this Question
Related Questions
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Get axis of bullet for rigidbody.velocity 1 Answer
Rigidbody velocity limiter 0 Answers
Rigidbody.velocity and Time.deltaTime 3 Answers
G Force Acceleration 0 Answers