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
1
Question by JustJunuh · Jun 19, 2015 at 07:58 AM · rotationraycastwallparallelcross

[SOLVED] How to set an object's rotation parallel to a wall.

The title says it all. I'm making a wall running mechanic for a third person platformer.

I'm basically trying to make it so when all the parameters are met and the "wallRun" method starts, I want the player to set it's rotation (just the y part) parallel to the wall so it will later translate forwards and not crash into the wall.

However I do not want to simply set the objects rotation equal to the wall's rotation. Later down the line, that won't work for what I'm planning. I need some sort of raycast / Vector3.Cross magic.

Since the player's rotation will vary when the "wallRun" method starts, I do not know how to raycast towards the wall. I do track if the wall is on the right or left side of the player via trigger hitboxes.

If I could raycast towards the closest point on the wall (at the same height) then raycast up, I could Vector3.Cross those and get a forwards vector parallel to the wall.

So basically, does anyone know how to raycast to the closest point on the wall? Or does someone have a better idea than what I'm thinking. (I'm sure someone does :P) Thank you in advance!

EDIT: This issue has been solved! :D Read the comments to see the details.

Comment
Add comment · Show 8
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 Hellium · Jun 19, 2015 at 08:10 AM 1
Share

What do you know about this wall precisely ?

If you have the normal vector of your wall, you can define a ray where the direction is in the opposite direction of the wall's normal vector.

Take a look at this website :

Wolfram $$anonymous$$athWorld : Point-Plane Distance

http://mathworld.wolfram.com/Point-PlaneDistance.html

avatar image JustJunuh · Jun 19, 2015 at 09:41 PM 0
Share

alt text

That is a poorly drawn example. So would that all work? I feel like the code would mess up at the cross part since the pink and green vectors aren't touching. Any ideas?

wallrunraycast.png (24.5 kB)
avatar image Hellium · Jun 19, 2015 at 10:06 PM 0
Share

I don't think it will mess up as you say.

The cross product doesn't $$anonymous$$d if the vectors are touching or not (a direction vector can be placed anywhere)

Have you tried to implement this solution ? Where do you face difficulties ?

avatar image JustJunuh · Jun 19, 2015 at 10:31 PM 0
Share

Lol I always thought this solution would fail so I didn't even attempt it. face palm I'm busy at the moment but I will implement the method when I get a chance and get back to you. Regardless thanks for your help! I greatly appreciate it good sir! (or ma'am)

avatar image JustJunuh · Jun 19, 2015 at 11:04 PM 0
Share

O$$anonymous$$. Found time to test it. Lol, wasn't expecting I was gonna have enough time. So all is fine EXCEPT the crossed vector sometimes crosses in the opposite direction. Is it possible to force the vector to always go "forward"? The movementmode means if it is right or left. 1 being right and 2 being left. I tried using a vector offset but that gave me more issues so thats why you see a redudnant section of "+ new Vector3()" I left it in for you. Here's my code:

 if (movement$$anonymous$$ode == 1)
 {
     RaycastHit hit;
     if (Physics.Raycast (transform.position + new Vector3(0f,0f,0f),transform.right,out hit,10f))
     {
       Vector3 temp = Vector3.Cross (transform.up,hit.normal);
       transform.rotation = Quaternion.LookRotation(-temp);
     }
 }


 if (movement$$anonymous$$ode == 2)
 {
     RaycastHit hit;
     if (Physics.Raycast (transform.position + new Vector3(0f,0f,0f),-transform.right,out hit,10f))
     {
        Vector3 temp = Vector3.Cross (transform.up,hit.normal);
        transform.rotation = Quaternion.LookRotation(-temp);
     }
 }

 
Show more comments

1 Reply

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

Answer by JustJunuh · Jun 21, 2015 at 11:29 AM

Solved. Read the comments for details.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Raycast on LocalCoordinates 1 Answer

Can't figure out how to make my raycast obstacle avoidance less jittery 3 Answers

How to make an object rotate to raycast hit from mouse? Unity 3D 1 Answer

Raycast Rotation on Steering Wheel 0 Answers

Rotate object to make it point towards raycast hit point. 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