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 alexdevAo · Sep 25, 2014 at 12:05 PM · c#addforcedice

adding a force to a dice and find out which face collided with the ground

I'm having trouble adding strength to a dice and make him go forward and then after a certain time. And also like to know what was the face that is glued to the floor in order to then pin down the value of the dice.

Sorry for the question. Because I am a beginner in programming. Sorry for my bad English.

using UnityEngine; using System.Collections;

public class Dice1Script : MonoBehaviour {

 public CubePlayerScript cubePlayerScript;
 public float forceMove = 5.0f;
 public GameObject face1;
 public GameObject face2;
 public GameObject face3;
 public GameObject face4;
 public GameObject face5;
 public GameObject face6;

 // Update is called once per frame
 void Update () {
     if (cubePlayerScript.launchedDice) {
         rigidbody.AddForce(forceMove * Time.deltaTime, 0, 0);
     }

     if (rigidbody.velocity == Vector3.zero) {
         if(Physics.Linecast(transform.position, face1.transform.position)){
             Debug.Log("The dice value is 6");
         }
     
     }
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Sep 25, 2014 at 12:14 PM

  • Typically you are rolling dice, you want to add force once, not every frame, and if you are adding force over multiple frames, then it should be done in FixedUpdate(), not Update. I don't see where cubePlayerScript.launchDice gets set, so I'm not sure how you are handling force.

  • Assuming you left the mass at 1.0, the amount of force you are applying is too small. Depending on your scale, typical values are for AddForce for a mass of 1.0 are in the 500 to 2000 range. Assuming 60 fps, your force is 5/60 = .083.

  • You likely want to do both an AddForce() and an AddTorque() so the dice both spins and moves.

As for telling which side is up, you can use Vector3.Dot() or Vector3.Angle(). If I calculate these six values:

 Vector3.Dot(transform.up, Vector3.up);
 Vector3.Dot(transform.right, Vector3.up);
 Vector3.Dot(transform.forward, Vector3.up);
 Vector3.Dot(-transform.up, Vector3.up);
 Vector3.Dot(-transform.right, Vector3.up);
 Vector3.Dot(-transform.forward, Vector3.up);

The highest one will be the side that is most upfacing. If the dice are on a flat surface, the value will be near or at 1.0 for the side facing up. Note the side facing down will have a value of near or at -1.0.

Comment
Add comment · Show 1 · 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 robertbu · Sep 25, 2014 at 03:09 PM 0
Share

Here is a script to get you started. The values on the sides of the dice are based on the texture at this link:

http://answers.unity3d.com/questions/458491/how-to-map-texture-in-a-cube.html

...with 1 being up and 3 being forward.

 using UnityEngine;
 using System.Collections;
 
 public class Dice : $$anonymous$$onoBehaviour {
 
     Vector3[] faces = {Vector3.up, Vector3.right, Vector3.forward, -Vector3.forward, -Vector3.right, -Vector3.up};
     bool rolling = false;
 
     void Update () {
     
         if (rolling && rigidbody.velocity.magnitude < 0.001f) {
             rolling = false;
             int i = WhichSide();
             if (i == -1) {
                 Debug.Log ("At an angle");
             }
             else {
                 Debug.Log ("Displaying value " + i);
             }
         }
 
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) {
             RollDice();
         }
     }
 
     void RollDice () {
         rolling = true;
         Vector3 force = Vector3.forward * Random.Range (400.0f, 600.0f) + Vector3.up * Random.Range (200.0f, 250.0f) + Vector3.right * Random.Range(-50.0f, 50.0f);
         rigidbody.AddForce (force);
 
         Vector3 v = Vector3.forward;
         v = Random.rotation * v;
         rigidbody.AddTorque (v * 25.0f);
     }
 
     int WhichSide() {
         for (int i = 0; i < faces.Length; i++) {
             if (Vector3.Dot(Vector3.up, transform.TransformDirection(faces[i])) > 0.8f) {
                 return i+1;
             }
         }
         return -1;
     }
 }

avatar image
0

Answer by sevensixtytwo · Sep 25, 2014 at 12:25 PM

It's much better to use AddForce with a derived direction, and using a ForceMode can dramatically change how it gets "thrown". Call this one in Start or on command, not in Update.

 void ThrowDice() {
 
     //Apply 10 units of force in the global Z-axis in one frame
     rigidbody.AddForce(Vector3.forward * 10, ForceMode.Impulse);
 
     }

ForceModes

And then check its velocity to see if it has stopped.

 void Update() {
 
     if (rigidbody.velocity.magnitude < 0.1) {
         CheckFace();
         }
 
     }

Now for the fun part. Raycast from all local axes in order to see which side is touching the ground. Might want to use a tag or something for this.

Comment
Add comment · Show 2 · 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 alexdevAo · Sep 25, 2014 at 02:37 PM 0
Share

rigidbody.AddForce(Vector3.forward * 10, Force$$anonymous$$ode.Impulse); This makes the Dice always go ahead. The tendency is to stop rolling in the given time

avatar image Habitablaba · Sep 25, 2014 at 03:12 PM 0
Share

You only need one raycast if you cast down from above the dice. This will give you the face that's up, and the opposite face is 'down'

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

AddForce mechanism ? 1 Answer

OnCollisionEnter Sine Wave to Character 0 Answers

Projectile not moving properly in top down 2d shooter 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