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 Tzvieyhuda · Feb 22, 2017 at 09:15 AM · transformtransform.positiondirectionreturnprint

want to find out what side of die is pointing up

First Post to this forum, first attempt at C# & Unity so I appreciate any/all feedback. I am trying to create a dice game and want to determine which side of the rigidbody has the highest Y directional axes and return that face value. I currently get a warning sign saying unreachable code detected and do not have any value returned for my y axes.

here is my current code:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ApplyForceInRandomDirection : MonoBehaviour {

 public string buttonName = "Fire1";
 public float forceAmount = 10.0f;
 public ForceMode forceMode;
 public Rigidbody rb;
 public float torqueAmount = 10.0f;
 public Vector3 dieDirection;



 void Start() 
 {
     rb = GetComponent<Rigidbody>();
     dieDirection = rb.transform.TransformDirection(0,0,0);
 }    
 // Update is called once per frame
 void Update () {

     
     if (Input.GetButtonDown (buttonName)) {
         rb.AddForce (Random.onUnitSphere*forceAmount, forceMode);
         rb.AddTorque (Random.onUnitSphere*torqueAmount, forceMode);
     }
 }
 public Vector3 GetDirection(float x, float y, float z){
     return transform.TransformDirection (dieDirection);
     print (y);
 }

}

Comment
Add comment · Show 4
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 Tourist · Feb 22, 2017 at 10:04 AM 0
Share

The unreachable code error is the print(y); because it is after the return of the function.

avatar image _Yash_ · Feb 22, 2017 at 12:10 PM 2
Share

Attach 6 empty objects as children of dice. Each object is at some distance from one of the faces of dice. When dice stops rolling, just find which empty object has highest Z position value. this way you will know which side corresponds to that empty object. you can name those objects as side numbers and parse the Name as an Int.

avatar image elenzil _Yash_ · Feb 22, 2017 at 07:22 PM 0
Share

this is a good approach. it also works for arbitrary-sided dice like D20. for D4 you'd want to look for the lowest face.

avatar image NoseKills _Yash_ · Feb 23, 2017 at 07:33 AM 0
Share

You could do without extra objects if you just have a list of the normals of your die's sides. Calculate Dot products with Vector3.up and the biggest number you get is the side pointing up.

With a 6 sided die this is especially easy because the normals are transform.forward, transform.right etc. (Assu$$anonymous$$ you use Unity's primitive cube or some other mesh that's axis aligned )

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KataSeiko · Feb 22, 2017 at 12:11 PM

With the dice, you should work with a random rotation and drop the dice from maybe 5 Unity units height onto a flat surface. In the real world, you let the dice drop from a certain height on e.g. a table. So it's best to copy this into the game world:

 public GameObject dice;
 public Vector3 dropLocation = new Vector3(0, 5, 0);
 public float tumble = 5f;
 
 public void RandomRotator()
 {
   dice.transform = dropLocation;
   RigidBody rb = this.GetComponent<RigidBody>();
   rb.angularVelocity = Random.insideUnitSphere * tumble;
 }

You can call this e.g. from a button and let Physics do the rest.

Afterwards, you can check the angles on the transform. A rotation of -90 and 90 on X would be the first two sides, -90 and 90 on the Z would be the other two, 0 and -180 on the Y would be the last two sides..

Your "unreachable code" is what you wrote after "return". Return can be used to end functions prematurely (e.g. "if (gameOver) { return; }; ). In this case, it ends the function before you call "print".

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

8 People are following this question.

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

Related Questions

How to move several objects "y" units during a fixed time 1 Answer

Difference between Vector.up and transform.up 2 Answers

Shooting script C# 1 Answer

transform.position returning wrong value (UNITY 2D) 0 Answers

In my gun script the bullets aren't hitting the correct area. 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