Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Dec 04, 2014 at 04:52 AM by Prasanna for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Prasanna · Feb 11, 2014 at 06:17 AM · c#animation3dvalue

Dice Value

Hello there,

I have code for calculating dice value... they using dot product to get the value, i want to know about the code. if anyone know about dot product pls explain me or anyone know how to calculate the dice value tell me the logic. Here is the code.

 using UnityEngine;
 using System.Collections;
 
 public class DiceValue : MonoBehaviour 
 {
     public int side=0;
     int CalcSideUp() 
     {
         float dotFwd = Vector3.Dot(transform.forward, Vector3.up);
         if (dotFwd > 0.99f) return 7;
         if (dotFwd < -0.99f) return 4;
         
         float dotRight = Vector3.Dot(transform.right, Vector3.up);
         if (dotRight > 0.99f) return 8;
         if (dotRight < -0.99f) return 3;
         
         float dotUp = Vector3.Dot(transform.up, Vector3.up);
         if (dotUp > 0.99f) return 5;
         if (dotUp < -0.99f) return 2;
         
         return 0;
     }
     
     void Update() 
     {
         int side = CalcSideUp();
         if (side > 0)
         {
             Debug.Log("Side = " + side);
         }
     }
 }

-Prasanna.

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

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Feb 11, 2014 at 06:27 AM

This code is using the dot product in a very specific way. What they are doing only works if all the vectors involved are normalized (i.e. of unit length). All the ones they are use are normalized. When two normalized vectors are pointing in the same direction, their dot product value will be 1.0 (give or take a bit due to floating point imprecision). If two vectors are pointing in opposite directions, then dot product will be -1.

The other thing going on here is the value of each side of the cube. Their cube has the following values:

  • front - 7

  • back - 4

  • right - 8

  • left - 3

  • top - 5

  • bottom - 2

So take a look at lines 9 - 11. First the forward vector is compared with world up. If they are the same (i.e. dotFwd > 0.99f), then you know the forward side is facing up. If the value is near -1, then you know the back side is facing up.

Lines 13-15 text left/right.

Lines 17-19 test top bottom.

In the unlikely event the cube is on the edge (i.e. it got hung up on something), the function returns 0.

Comment
Add comment · Show 8 · 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 Prasanna · Feb 11, 2014 at 06:32 AM 0
Share

hi robertu, its working perfectly the cube without textures.. but i have a model with texture, so the problem is this code is not mixed with that texture.. basically am a designer. so i dont know about coding part. -Prasanna

avatar image Prasanna · Feb 11, 2014 at 06:33 AM 0
Share

If u know how to do this in another way. plz tell me. i want the code in c#..

-Prasanna

avatar image robertbu · Feb 11, 2014 at 06:37 AM 0
Share

Bring your textured cube into unity. Set the rotation to 0.0. In Unity the front is the size fazing positive 'z'. So in the code above adjust the values for your cube. I've listed all the values and how they match to the sides, so you should be able to see what lines to change. For example, if your front side has the value of 2, then change the '7' to '2' in the code above. If your bottom is '5' then change the '2' in the code above to '5'.

avatar image Prasanna · Feb 11, 2014 at 06:41 AM 0
Share

thanks for the tip.. will try and tell

-Prasanna

avatar image Prasanna · Feb 11, 2014 at 06:50 AM 0
Share

Hi robertu, i think i made mistake with my animated object, i created my object starting rotation is 0,0,45.

Show more comments

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Animation will only play once with mouse click and key press, i want it to play anytime these are pressed together.,animation will only play once, but wont play at all if another animation plays first. 0 Answers

Animation/CrossFade difficulties, Animation won't CrossFade 0 Answers

How do I make a spotlight make a model play an animation? 2 Answers

Door animation bugs and stays open unity 3D 0 Answers

Editing alpha through a script and animation changes the colour not the transparency? 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