• 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 m0mohy92 · Dec 06, 2018 at 01:43 PM · collisionmovementrigidbodyaddforceaccelerometer

Trouble in freezing movement in z axis

Hi everyone! I am making a mobile serious game for forearm rehabilitation; one of the exercises depend on arm extension then moving the arm in the x axis, a ball represents the arm movement and a target "brick" should be hit. I used the script below to firstly achieve only z axis movement, then when reaching a specific distance allowing only movement in x axis. To achieve the exercise every time the ball hits the brick, the ball is retrieved to start position, but what happens is that when the ball returns to the start position, movement is allowed in x axis, however i disabled it in the code. Any help?

 void FixedUpdate () {
             float x = Input.acceleration.x;
             float z = Input.acceleration.y;
             Vector3 CubePosition = Cube.position;
             Vector3 PlayerPosition = transform.position;
             dz = Mathf.Floor(CubePosition.z-PlayerPosition.z);
             dx = Mathf.Floor (CubePosition.x-PlayerPosition.x);
             if (dz > 0) {
                 rb.AddForce (0, 0, z * mainSlider.value);
             }
                 else {
                     rb.AddForce (x * mainSlider.value, 0, 0);
                  
             }

The code is divided between two scripts, one attached to the ball which is above, the other attached to the brick :-

 private void OnCollisionEnter (Collision collision)
     {
         
         if (gameObject.tag == "Brick") {
 Ball.GetComponent<Renderer> ().enabled = false;
         Ball.transform.position = new Vector3 (.43f, 0.42f, -.2f);
         Ball.transform.Translate (0, 0, Input.acceleration.y);
         Ball.GetComponent<Renderer> ().enabled = true;
 }
 }


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

1 Reply

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

Answer by Vega4Life · Dec 06, 2018 at 04:23 PM

Just a guess, but it may be because the ball still has velocity in those directions when it collides. All you are doing is just resetting its position, but it will maintain some velocity.


Try resetting the velocity in the OnCollision function:


  private void OnCollisionEnter (Collision collision)
      {
          
          if (gameObject.tag == "Brick") {
  Ball.GetComponent<Renderer> ().enabled = false;
          Ball.transform.position = new Vector3 (.43f, 0.42f, -.2f);
          
          // Reset velocity \\
          Ball.rigidbody.velocity = Vector3.zero;  // this is just pseudo to give you an idea
 
          Ball.transform.Translate (0, 0, Input.acceleration.y);
          Ball.GetComponent<Renderer> ().enabled = true;
  }

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 m0mohy92 · Dec 06, 2018 at 05:16 PM 0
Share

Woohoo! It worked perfectly, thank you

avatar image Vega4Life m0mohy92 · Dec 06, 2018 at 05:27 PM 0
Share

yay! Good luck.

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

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

Related Questions

i want to make an animation throw code 0 Answers

Box collider (with Rigidbody attached) gets stuck into another Box Collider 0 Answers

How to make an object move in an arch 1 Answer

Player getting stuck in ground (3D) player has Rigidbody, and Box Collider, world is Mesh Colliders 0 Answers

Keep Horizontal Momentum after Jump 2 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