• 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 sub0 · Jan 29, 2012 at 10:08 PM · rotatemoveballrollmarble

How to roll a ball ?

Hi guys,

I am making a game where the main player is a ball and I want to make the ball roll when I move it. How can I make that? What script should I write?

Thanks.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by POLYGAMe · Jan 29, 2012 at 10:18 PM

A simple way would be to use transform.Translate as well as transform.Rotate, so that the ball rotated as it moved:

 // Moves ball along the X and rotates on the z to simulate rolling
 transform.Translate(5, 0, 0);
 transform.Rotate(0, 0, 5);

Obviously that's REALLY basic and you'd need to tweak the math but shouldn't be too hard to get going.

Comment
Add comment · Show 4 · 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 POLYGAMe · Jan 29, 2012 at 10:19 PM 0
Share

Or, you could use Unity's physics... I haven't touched much of that but addforce could do it... would roll the ball for you.

avatar image sub0 · Jan 29, 2012 at 10:27 PM 0
Share

Thanks. But It didn't work. The ball just keeps rotating and going down forever. I want it to roll when I move it like in a terrain.

avatar image POLYGAMe · Jan 29, 2012 at 10:34 PM 0
Share

Like I said, you'd need to tweak it. $$anonymous$$ake variables for speed and rotation (ins$$anonymous$$d of using "magic numbers" and have the rotation match the speed. Then when it's not moving it won't roll.

avatar image Varazdt · Jun 28, 2014 at 01:00 PM 0
Share

I am sorry it is probably to write Get$$anonymous$$ey

avatar image
3

Answer by Varazdt · Jun 28, 2014 at 01:00 PM

The right way to roll the ball it is roll ball to the way with your camera will faced...So for this I wrote the code on c# witch will move the ball always in right way,the way witch look your camera..Sooo

public float ballSpeed = 10.0f;

void FixedUpdate(){

 if(input.GetKeyDown(KeyCode.W)){
    this.rigidbody.AddForce(Camera.main.transform.forward * ballSpeed);
 }

  if(input.GetKeyDown(KeyCode.S)){
    this.rigidbody.AddForce(-Camera.main.transform.forward * ballSpeed);
 }

  if(input.GetKeyDown(KeyCode.A)){
    this.rigidbody.AddForce(-Camera.main.transform.right * ballSpeed);
 }

  if(input.GetKeyDown(KeyCode.D)){
    this.rigidbody.AddForce(Camera.main.transform.right * ballSpeed);
 }

}

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
avatar image
0

Answer by shahar3447 · Sep 28, 2014 at 03:36 PM

add to the sphere a rigid body and use this code:

 public float speed;
     void FixedUpdate (){
 
     float moveHorizontal = Input.GetAxis("Horizontal");
 
     float moveVertical = Input.GetAxis("Vertical");
 
     Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
 
     rigidbody.AddForce(movement * speed * Time.deltaTime);
     }
 
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Rolling mable... rigid body? 1 Answer

Making a ball roll and keeping the axis? 2 Answers

pong rolling ball 1 Answer

camera focus on sphere 1 Answer

moving a ball when touch 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