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 Nine · Nov 10, 2013 at 07:13 PM · rotationtransformvector3normalsorientation

Make Gameobject Stand On Surface Facing Certain Direction

I want to make a biped character stand on any surface I click on.

Surfaces have up vectors of any of positive or negative X,Y,Z. So imagine a cube with each face being a gameobject whose up vector pointing directly away from the cube.

If my character is facing "forward" and I click on a surface which is to the left or right of me ( left or right walls), I want my character to now be standing on that surface but still be facing in the direction he initially was. If I click on a wall which is in the forward path of my character i want him to now be standing on that surface and his forward to now be what was once "up" relative to my character. Here is the code I am working with now.

 void Update()
 {
     if (Input.GetMouseButtonUp (0)) {
         RaycastHit hit;
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, out hit)) {
             Vector3 upVectBefore = transform.up;
             Vector3 forwardVectBefore = transform.forward;
             Quaternion rotationVectBefore = transform.rotation;
 
             Vector3 hitPosition = hit.transform.position;
             transform.position = hitPosition;
             float lookDifference = Vector3.Distance(hit.transform.up, forwardVectBefore);
 
             if(Vector3.Distance(hit.transform.up, upVectBefore) < .23) //Same normal
             {
                 transform.rotation = rotationVectBefore;
             }
             else if(lookDifference > 1.412 && lookDifference <= 1.70607) //side wall
             {
                 transform.up = hit.transform.up;
                 transform.forward = forwardVectBefore;
             }
             else //head on wall
             {
                 transform.up = hit.transform.up;
                 transform.forward = upVectBefore;
             }
         }
     }
 }

The first case "Same normal" works fine, however the other two do not work as I would like them to. Sometimes my character is laying down on the surface or on the wrong side of the surface. Does anyone know nice way of solving this problem?

Comment
Add comment · Show 1
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 sdgd · Nov 10, 2013 at 07:16 PM 0
Share

well, khm khm khm, ... did you even search?

what you need is:

  • http://answers.unity3d.com/questions/558848/moon-player-movement.html

OR even more:

  • http://answers.unity3d.com/questions/557766/how-to-move-around-a-sphere.html

as I'm in good mood I'm going to even copy paste the code here.

 Ray ray = new Ray(transform.position, Planet.position - transform.position));
 RaycastHit hit;
 if (Planet.collider.Raycast(ray, out hit, Vector3.Distance(Planet.position, transform.position) )  ){
     transform.rotation = Quaternion.LookRotation( Vector3.Cross(transform.right, hit.normal) , hit.normal);
 }

OFC the Planet.position is your object from witch you want to be around.

NOTE if you have a mesh collider and it will be a bit uneven you won't be facing towards the center of it.

0 Replies

· Add your reply
  • Sort: 

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

17 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Connecting transform with Vector3 4 Answers

Change transform.forward while slerping rotations around the transform.forward axis 1 Answer

Object wont rotate in the opposite direction 1 Answer

How to make Y-Axis face away from a position? 2 Answers

How do I make gameObject.transform.rotation.z equal to a set float value? 2 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