• 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 claytoncurmi · Aug 30, 2011 at 10:37 AM · collisionphysicsrigidbodyballmaze

Moving a ball around a maze

Hi everyone,

I'm working on a maze-like game, whereby the player has to steer a ball without being detected by guards or cameras! The ball has a kinematic rigidbody and a sphere collider attached to it while the maze itself has a mesh collider attached to it. The problem is that whenever I move the ball towards the wall, the ball simply passes through! Shouldn't the engine automatically detect that there is a collision and stops the ball from passing through the wall? I tried various collision detection methods (continuous, discrete etc). I also tried switching off the isKinematic option but in the documentation there is specifically written that the position of rigidbodies can't be directly modified by transforms.

Should a script take care of this? Or this can be done natively within Unity? How should I go about this? Any help will be greatly appreciated! Thanks for your assistance.

Here is the simple script attached to the ball gameobject.

void Update () {

 transform.Translate(0f, _floatHeight*Mathf.Sin(_floatFrequency*Time.time), 0f);

 if(Input.GetKey(KeyCode.UpArrow))
     transform.Translate(Vector3.forward * 0.5f);

 if(Input.GetKey(KeyCode.DownArrow))
     transform.Translate(Vector3.back * 0.5f);

 if(Input.GetKey(KeyCode.LeftArrow))
     transform.Translate(Vector3.left * 0.5f);

 if(Input.GetKey(KeyCode.RightArrow))
     transform.Translate(Vector3.right * 0.5f);

}

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
1

Answer by Waz · Aug 30, 2011 at 11:21 AM

You're using a Kinematic body. That means you decide it's position, not the physics engine, so it will happily let you embed it in a wall.

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
-1

Answer by Uniquesone · Aug 30, 2011 at 11:26 AM

transform.Translate

Doesnt detect collisions..

Try to add a force or use transformdirection! http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddForce.html http://unity3d.com/support/documentation/ScriptReference/Transform.TransformDirection.html

if(Input.GetKey(KeyCode.UpArrow)) rigidbody.AddForce(Vector3.forward * 0.5f);

if(Input.GetKey(KeyCode.DownArrow)) rigidbody.AddForce(Vector3.forward * -0.5f);

if(Input.GetKey(KeyCode.LeftArrow)) rigidbody.AddForce(Vector3.right * -0.5f);

if(Input.GetKey(KeyCode.RightArrow)) rigidbody.AddForce(Vector3.right * 0.5f);

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 Waz · Aug 30, 2011 at 11:34 AM 0
Share

You can't AddForce to a $$anonymous$$inematic Rigidbody.

avatar image Uniquesone · Aug 30, 2011 at 11:44 AM 0
Share

"I also tried switching off the is$$anonymous$$inematic option"

Then it should work =)

avatar image
0

Answer by aldonaletto · Aug 30, 2011 at 01:33 PM

A kinematic rigidbody isn't under physics influence, as @Warwick Allison said. You should uncheck isKinematic and useGravity (since your ball floats). It's also better to store the initial height and add Mathf.Sin to it - if you use Translate, it can accumulate errors and after some time the ball may be landed or too high. Try this script:

public float _floatHeight = 0.1f; public float _floatFrequency = 3f; public float speed = 5f; float y0;

void Start(){ rigidbody.isKinematic = false; rigidbody.useGravity = false; y0 = transform.position.y; }

void Update() { Vector3 pos = transform.position; pos.y = y0+_floatHeight*Mathf.Sin(_floatFrequency*Time.time); transform.position = pos; Vector3 dir = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"))*speed; rigidbody.velocity = dir; // set the horizontal velocity according to controls }

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

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

Camera gets flung off of the map when the player collides with certain objects. 0 Answers

Rigidbody necessary to use collision detection 1 Answer

Nav Mesh Agent won't fall off the platform? 1 Answer

Ball physics help 2 Answers

Character rigidbody collisions 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