• 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
1
Question by Dr_GeoFry · Apr 12, 2012 at 11:20 PM · androidmovementphysicsjumping

Physics on a Ball

Hi, I have a ball that I want to properly display physics. What I mean by this is that I want to ball to roll properly from left to right, and then, if I change direction, I want the momentum to slow the ball down and accelerate from right to left. Additionally, when I jump, I don't want the ball to fall from left to right and right to left solely based on the user rocking the phone back and forth. Right now, I am having trouble with both the physics of movement and the physics of the ball in the air

My code for movement:

function Update () {
 var dir : Vector3 = Vector3.zero;
 dir.x = -Input.acceleration.y.5;
 transform.position.x += dir.x;
 //dir.y = -Input.acceleration.x.5;
 //transform.position.y += dir.y; }
My code for jumping:

var dir : Vector3 = Vector3.zero; var isFalling : boolean=true;

function Update () { var fingerCount = 0; for (var touch : Touch in Input.touches) { if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) fingerCount++; } if (fingerCount > 0) //&& !isFalling){ rigidbody.velocity.y=7; } isFalling=true; function OnCollisionStay(collisionInfo : Collision) { isFalling=false; }

Comment
Add comment · Show 2
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 rutter · Apr 13, 2012 at 01:20 AM 0
Share

What specific problem are you trying to fix? If you're just asking a general question, you should probably check the Unity physics manual or look for other tutorials, first.

avatar image Dr_GeoFry · Apr 13, 2012 at 01:24 AM 0
Share

I guess I was asking too many questions at once, so I'll just focus on getting the ball movement to operate correctly. I've changed the constraints of the rigidbody, but when I run this on my phone, there is no rolling. Additionally, when I change direction, there is no slow down and readjustment to the change in direction. So, I'd like assistance with what I must have done wrong on that issue.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Apr 13, 2012 at 01:28 AM

You can't use physics and move by modifying transform.position!. You should use rigidbody.AddForce to move the ball - something like this:

var force: float = 5.0;

function Update () { var dir : Vector3 = Vector3.zero; dir.x = -Input.acceleration.y; dir.z = Input.acceleration.x; rigidbody.AddForce(dir * force); } To jump, set the rigidbody.velocity.y to the jump speed, as you seem to be already doing.

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 Dr_GeoFry · Apr 13, 2012 at 01:41 AM 0
Share

thanks! I'll give it a try.

avatar image Dr_GeoFry · Apr 13, 2012 at 04:02 AM 0
Share

it worked great. Thank you for your expertise.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

How do you calculate DeltaX and DeltaZ for a character? 1 Answer

physics.gravity with tilt game 1 Answer

Movement Script for Player... 1 Answer

How to check if object is on ground (C#)? 1 Answer

Jump Script for 2D (JavaScript) 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