Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tea_Doogun · Oct 08, 2012 at 06:52 PM · rigidbodyraycastvelocity

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 :)

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tea_Doogun · Oct 08, 2012 at 07:17 PM 0
Share

$$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$$ :)

avatar image Tea_Doogun · Oct 08, 2012 at 07:51 PM 0
Share

I've never used Vector3Project before, so i can't figure this last bit out (BUT I FEEL SO CLOSE!):

Error: $$anonymous$$ identifier: 'r'

2 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Muuskii · Oct 08, 2012 at 07:57 PM 1
Share

Where is the raycast in this answer?

avatar image Tea_Doogun · Oct 08, 2012 at 07:58 PM 1
Share

FTW!!

Thanks for your patience, man :D

avatar image Tea_Doogun · Oct 08, 2012 at 08:00 PM 0
Share

And thanks for your input and time, $$anonymous$$uuskii :)

avatar image Tea_Doogun · Oct 08, 2012 at 08:02 PM 1
Share

I put the Vector3.Project into a simple raycast script, works well :)

avatar image Muuskii · Oct 08, 2012 at 08:37 PM 0
Share

"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.

Show more comments
avatar image
0

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?

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tea_Doogun · Oct 08, 2012 at 07:29 PM 0
Share

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.

avatar image Muuskii · Oct 08, 2012 at 07:29 PM 0
Share

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.

avatar image Tea_Doogun · Oct 08, 2012 at 07:30 PM 0
Share

Your description is correct:

JUST stop velocity in the direction of the nose of the object.

avatar image Muuskii · Oct 08, 2012 at 07:36 PM 0
Share

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.

avatar image Muuskii · Oct 08, 2012 at 07:50 PM 1
Share

@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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges