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 Leon.S.Kennedy · Aug 26, 2012 at 06:44 AM · physicsrotatemovespherefall

Making a sphere move and rotate like a real ball on ground

Im a newbie to unity, so here goes my question.. In the scene, i have 4 objects, a Cube, a sphere, Main Camera And A directional light. "Mouse Orbit" script(found in wiki unity site) is attached to the Main Camera and its working fine. Cube is of dimension (50,5,50) and located 30 units below the sphere. I added rigid body to the cube and turned 'Use Gravity' OFF. Finally Sphere with rigid body and 'use gravity' ON and the following script is attached to it.. Now the problem is when i run the game, the sphere keeps falling through the cube, but i want it to stay on the cube so that i can move and jump around... Please Help... Im kinda stuck here..

 // speed of the ball
 var speed = 5.0;
 var radius = 1.0;
 private var velocity : Vector3 = Vector3.zero;
 
  
 function Start(){
     transform.localScale = Vector3.one * radius * 2;
     var hit : RaycastHit;
     if(Physics.Linecast(transform.position, transform.position - Vector3.up * 500, hit)){
         transform.position = hit.point + Vector3.up * radius;
     }
     // add a rigidbody if we dont have one.
     if(!rigidbody)
         gameObject.AddComponent(Rigidbody);
     // set the mass according to the radius.
     rigidbody.mass = 100 * radius;
 }
  
 function FixedUpdate () {
     // let see if our body is on the ground.
     var hit : RaycastHit;
     var isGrounded = Physics.Raycast(transform.position, -Vector3.up, hit, radius * 1.5);
     
     // base movement off of the camera, not the object.
     // reset the camera's X to zero, so that it is always looking horizontally.
     var x = Camera.main.transform.localEulerAngles.x;
     Camera.main.transform.localEulerAngles.x = 0;
     
     // now collect the movement stuff This is generic direction.
     var direction = Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
     
     // prevent the ball from moving faster diagnally
     if(direction.magnitude > 1.0) direction.Normalize();
     
     // If we are grounded, then lets see if we want to jump.
     if(isGrounded && Input.GetKeyDown(KeyCode.Space))
         rigidbody.AddForce(Vector3.up * rigidbody.mass * 500);
     
     // if we arent pressing anything, dont mess with the physics.
     if(direction.magnitude > 0){
         // convert isGrounded into something we can use
         var modifier = isGrounded ? 3.0 : 0.5;
         // lets set the direction according to the camera now.
         direction = Camera.main.transform.TransformDirection(direction) * speed * 2;
         // lets take the downward velocity from the current so that we dont get wierd physics results
         direction.y = rigidbody.velocity.y;
         
         // Now, lets keep track of a velocity.
         // This will let the ball move while we are not pressing anything.
         rigidbody.velocity = Vector3.Lerp(rigidbody.velocity, direction, modifier * Time.deltaTime);
         // Now, lets break the rotation out from the movement.
         var rotation = Vector3(rigidbody.velocity.z,0,-rigidbody.velocity.x) * 20;
         
         
         // Lets add some spin to make the ball move better
         rigidbody.angularVelocity = Vector3.Lerp(rigidbody.angularVelocity, rotation, modifier * Time.deltaTime);
     }
   
     
     // return the camera's x rotation.
     Camera.main.transform.localEulerAngles.x = x;
 }

And btw i didnt create this code, i found it in unity forums...

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
0
Best Answer

Answer by DragonSaige · Aug 26, 2012 at 11:14 AM

From the text it appears that you apparently don't have a collider applied to your cube. To do it now, follow these steps.

  1. Select your cube.

  2. Go to Component>Physics>Box Collider.

Also make sure your collider has isTrigger off.

Comment
Add comment · Show 3 · 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 Leon.S.Kennedy · Aug 26, 2012 at 11:58 AM 0
Share

Thank you very much, That worked.. All problems solved...

THank you again...

avatar image Leon.S.Kennedy · Aug 26, 2012 at 12:05 PM 0
Share

I figured the problem with my previous cube, i had added rigid body to that cube and froze rotation and disabled gravity for it... That caused the problem... Silly me... Thank yu for ur help

avatar image DragonSaige · Aug 26, 2012 at 12:42 PM 0
Share

Glad I was helpful, please press the tick mark button against my answer to mark this answer as valid

avatar image
0

Answer by BobbleHead · Aug 26, 2012 at 11:35 AM

     if (Input.GetKey ("right")) {
         rigidbody.AddForce (Vector3.forward * 10);
         }
 
     if (Input.GetKey ("left")) {
         rigidbody.AddForce (-Vector3.forward * 10);
         }
     }

Adding force will give your ball a roll. Perhaps not the best way to go about it, but it's worked for me in the past.

Just add this code to an update function and apply to a sphere, may need a rigidbody.

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

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

Rotate multiple objects around or rather move them on the surface of another object 1 Answer

Rotate and move the sphere while the up key is pressed 1 Answer

Move sphere with independent orientation 1 Answer

Compound Collider Moving Parts? 4 Answers

Make sphere rotate when controlled 7 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