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 wannabee · Dec 17, 2012 at 10:56 PM · animationrotationaxisnewbie

Make ball flip around cube...

Imagine a 1x1x1-cube. On top of that place a sphere with diameter 1. I would like to able to roll this ball around the sides of the cube. Ie. when you press "right", the ball will roll to the edge (ie. 0.5 units) and then rotate around the edge (90 degrees), and go down to the middle (again 0.5 units). In that way in ends at the right side of the cube. Press "right" again will roll the ball to the lower side of the cube. And so forth.

Of course I would also like to to be able to press "up" at any point and roll forward, and so forth.

I am very new to Unity so I have no idea how to do this in a smart way. I have tried manually coding it, but the code becomes horrible, and furthermore, it's not even working properly. I think, somehow, I misunderstand how the local axis rotate.

YOU DO NOT NEED TO READ THE FOLLOWING SCRIPT, a good answer hopefully does not include my code :) - but, the code does almost work, so if someone would try and open Unity, create a cube at (0,0,0) with scale(1,1,1), and a cube at (0,1,0), rotation (0, 90, 0), and scale (1,1,1), and apply this code to the sphere, you can see what I mean...

The code is horrible though, I know. Especially the way I have to correct for minor rotation mistakes.

ANY advice will be highly appreciated.

 #pragma strict
 
 static var doingRoll : boolean = false;
 
 private var speed : float = 10.0;
 private var radius : float = 0.5;
 
 private var angularScale : float = 180 / (Mathf.PI * radius) ;
 
 private var moved : float = 0;
 private var rotated : float = 0;
 private var moved2 : float = 0;
 
 private var corner : Vector3;
 private var finalPos : Vector3;
 private var finalRot : Vector3;
 private var moveVec : Vector3;
 
 private var axis : String;
 
 function Update () {
     if (Input.GetKeyDown("d") && !doingRoll) {
         doingRoll = true;
         axis = "right";
         initRoll();
     } else if (Input.GetKeyDown("a") && !doingRoll) {
         doingRoll = true;
         axis = "left";
         initRoll();
     } else if (Input.GetKeyDown("w") && !doingRoll) {
         doingRoll = true;
         axis = "forward";
         initRoll();
     }
     
     if (doingRoll) {
         roll();
     }
 }
 
 function initRoll() {
     moved = 0;
     rotated = 0;
     moved2 = 0;
     
     finalRot = transform.eulerAngles;
     if (axis == "right") {
         corner = transform.position + radius * (transform.right - transform.up);
         finalPos = transform.position + 2 * radius * (transform.right - transform.up);
         finalRot.z = finalRot.z - 90;
         moveVec = transform.forward;
     } else if (axis == "left") {
         corner = transform.position + radius * (-transform.right - transform.up);
         finalPos = transform.position + 2 * radius * (-transform.right - transform.up);
         finalRot.z = finalRot.z + 90;
         moveVec = -transform.forward;
     } else if (axis == "forward") {
         corner = transform.position + radius * (transform.forward - transform.up);
         finalPos = transform.position + 2 * radius * (transform.forward- transform.up);
         finalRot.x = finalRot.x + 90;
         moveVec = -transform.right;
     }
 }
 
 function roll() {
     if (moved < radius) {
         transform.Translate(moveVec * speed * Time.deltaTime);
         moved += speed * Time.deltaTime;
         return;
     } else if (rotated < 90) { // 90 degrees
          transform.RotateAround (corner, moveVec, - speed * angularScale * Time.deltaTime);
          rotated += speed * angularScale * Time.deltaTime;
          return;
     } else if (moved2 < radius) {
         transform.Translate(moveVec * speed * Time.deltaTime);
         moved2 += speed * Time.deltaTime;
         return;
     } else {
         doingRoll = false;
         transform.position = finalPos;
         transform.eulerAngles = finalRot;
         return;
     }
 }
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

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

9 People are following this question.

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

Related Questions

Object won't rotate z axis on second click? 2 Answers

Need help with rotation script. 1 Answer

Model stays at the end of my animation D:? 0 Answers

wrong rotation in animation per script 0 Answers

Trying to play animation made inside unity, with script, but it can't be found. 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