Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 gangafinti · Nov 06, 2015 at 03:13 PM · cross

Getting the cross product of vertices

Having problems with the cross product:

I am trying aim my player to the most nearby face of a cube so I wrote a script that detects the 3 most nearby vertices and then gives back the cross product between those vertices.

My problem is that when I start the game it places the player perfectly on the face but once I start moving the player my cross product function give back a vector of (0.0f,0.0f,0.0f). And no matter what I do the cross product funtion will always keep returning (0.0f,0.0f,0.0f);

Here is a demonstration of the problem:

https://www.youtube.com/watch?v=IDB3XC7ViuU&feature=youtu.be

At first it works fine, untill I move the player then it doesn't do anything anymore because NormalTri() doesn't return a vector with a magnitude higher then 0.

This is the funtion where the problem lies:

 Vector3 NormalTri()
 {
     Vector3 infinity = new Vector3(Mathf.Infinity,Mathf.Infinity,Mathf.Infinity);
     Vector3[] tri = new Vector3[]{infinity,infinity,infinity};
     int[] prevIndexes = new int[]{-1,-1,-1};

     for(int i = 0; i < 3; i++)
     {
         float magnitude = Mathf.Infinity;
         int saveIndex = 0;
         for(int ii = 0; ii < groundVerts.Length; ii++)
         {
             bool doubleCheck = false;
             foreach(int p in prevIndexes)
             {
                 if(ii == p)
                 {
                     doubleCheck = true;
                 }
             }

             if((groundVerts[ii] - obj.position).magnitude < magnitude && !doubleCheck)
             {
                 magnitude = (groundVerts[ii] - obj.position).magnitude;
                 saveIndex = ii;
             }
         }
         prevIndexes[i] = saveIndex;
         tri[i] = groundVerts[saveIndex];
     }
     Vector3 sideA = tri[1] - tri[0];
     Vector3 sideB = tri[2] - tri[0];
     return Vector3.Cross(sideA,sideB).normalized;

 }

groundVerts = The verts of the cube that keep being refreshd every update.

obj = the transform of the player.

Ground verts and obj are alway correctly stored so the problem cannot be that the is info missing.(tested that)

Also as a side question:

How do I know if my cross product points outwards of the cube and not inwards?

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 Glurth · Nov 06, 2015 at 04:11 PM 0
Share

How do I know if my cross product points outwards of the cube and not inwards?

Cross product will always follow the "right hand rule". So if A x B points outward B X A will point the opposite direction. But if that's not what you are looking for, perhaps this is... Adding either vertex used to the crossproduct, then subtract the origin of the cube (crossprod + vertex - cubeorigin). The result will yield a vector with magnitude this is : LARGER than the (vertex - cubeorigin) if it's pointing outward, and S$$anonymous$$ALLER that the (vertex-cubeorigin), if pointing inward. You could probably even use model coordinates, rather than world, to eli$$anonymous$$ate that cubeorigin subtraction. In this case though, it would probably be wise to normalize both the vertex and cossproduct_result before adding them.

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

31 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 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

Animating clip with transitions 2 Answers

Steps to determine cause of AOT Cross Compile Failed 1 Answer

Can't move a "cross platform input" object. 0 Answers

Adjusting GUI 1 Answer

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


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