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 prattfall · Mar 09, 2014 at 01:26 PM · rotationaddforcecoordinates

Mysterious behaviour of moving sphere

I'm trying to write a first simple game, as a learning exercise. So far, it's just a small ball rolling randomly on a large plane. It is more or less working. But I have a couple of questions about its behaviour which seems completely incomprehensible to me. Also one question about the syntax. What I wanted to do was randomly rotate the sphere around the y axis at intervals and then move the ball forward, so it's changing direction randomly. Here is the movement code:

     float directionChange = new float ();
     directionChange = (Random.Range (-rotationRange, rotationRange));
     transform.Rotate (0.0f, directionChange, 0.0f);
     rigidbody.AddForce (transform.forward * speed * Time.deltaTime);

  1. The sphere is 0.5 radius, positioned at 0.25 on the y axis. It is non kinematic and acting under gravity, yet looking at the sphere in the inspector when its running, the y coordinate is dithering randomly between about 0.2299 and 0.40. I can't see anything in the code that would be affecting this value at all. When I check contrain y position, the sphere stops moving altogether.

  2. The sphere is not just randomly changing direction, but also randomly changing speed, which I don't want. As far as I can see, I am only applying an unchanged force in the forward direction, so how can it be changing speed?

  3. The syntax question, I understood the first two lines of code above to be equivalent to:

    float directionChange = new float (Random.Range (-rotationRange, rotationRange));

but when I write this, I get the compiler error,

 The type `float' does not contain a constructor that takes `1' arguments

I'm obviously misunderstanding something. Any help on any of these questions would be welcome. Thanks.

Comment
Add comment · Show 4
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 whydoidoit · Mar 09, 2014 at 01:27 PM 0
Share

What is the mass of the sphere?

avatar image prattfall · Mar 09, 2014 at 01:34 PM 0
Share

Default 1

avatar image Destran · Mar 09, 2014 at 03:41 PM 0
Share

$$anonymous$$aybe you know this, but unity actually did a tutorial on a rolling-ball type game. If you're interested here it is:

http://unity3d.com/learn/tutorials/projects/roll-a-ball

avatar image prattfall · Mar 09, 2014 at 06:49 PM 0
Share

Yes, thanks, I just finished that tutorial and wanted to build on it.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by whydoidoit · Mar 09, 2014 at 01:45 PM

Ok so the ball is rolling which means that sometimes it's transform is facing downwards and sometimes upwards. As you add the force it is being pushed into the ground or push up away from it.

You are also adding force, this is increasing the speed of your sphere, apparently at random due to the amount it pushes into the ground.

You should calculate your random force like this:

   float directionChange = Random.Range(-rotationRange, rotationRange);

Then perhaps

   var forceDirection = Quaternion.AngleAxis(transform.eulerAngles.y + directionChange, Vector.up) * Vector3.forward;

This takes the current y rotation and adds on the direction change. If you don't have any friction then the ball will continue to accelerate as you continue to add force.

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 prattfall · Mar 09, 2014 at 03:36 PM 0
Share

I see, that makes a lot of sense. I'll try that. Thanks very much for your help!

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

22 People are following this question.

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

Related Questions

AddForce to parent based on child's rotation C# 1 Answer

Instantiating bullet and moving forward with rigidbody 0 Answers

Prevent Rigidbody From Rotating 3 Answers

Airplane goes weird on collision (Rigidbody) 1 Answer

Rotating Camera, and Movement help. 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