Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Levike · Dec 19, 2015 at 12:08 PM · rigidbody2dmovepositionrigidbody-collision

Ball bounces too much from AI collision

I want to create a simple pong game with an AI as an enemy. Everything is good until I add the AI movement script to the racket. The problem is as soon as the ball hits the racket it behavs like it has a bounciness 1000 or it got 1000 force. So the ball speed gets suddenly too much.

Here is the code: using UnityEngine; using System.Collections;

 public class AIMovement : MonoBehaviour {
     public GameObject ball;
     public float speed;
 
     private Rigidbody2D rb2D;
 
     // Use this for initialization
     void Start () {
         rb2D = GetComponent<Rigidbody2D> ();
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         Vector2 move = new Vector2 (ball.transform.position.x, 0.0f);
 
         rb2D.MovePosition (move);
     }
 }

If I add code from manual the bounce behaves like it should but the movement of the racket behaves like it has 3000 ping.

 rb2D.MovePosition(rb2D.position + velocity * Time.fixedDeltaTime);

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

2 Replies

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

Answer by Levike · Dec 20, 2015 at 08:58 AM

So I found a workaround for this. Here is the code. Speed is for making the AI beatable.

 using UnityEngine;
 using System.Collections;
 
 public class AIMovement : MonoBehaviour {
     public Transform ball;
     private float speed;
     public float distance;
 
     private Rigidbody2D rb2D;
 
     void Start(){
         rb2D = GetComponent<Rigidbody2D> ();
     }
 
     void FixedUpdate(){
         speed = Time.fixedDeltaTime * 40;
 
         if ((transform.position.y - ball.position.y) >= distance) {
             rb2D.MovePosition(new Vector2(ball.position.x, 0.0f) * speed);
         }
     }
 }

Basically I had to put the MovePosition to an if statement and the minimum distance must be set to 0.6. Below that the weird thing is happening;

I still don't know why it happens tho.

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 Veerababu.g · Dec 19, 2015 at 12:30 PM

the force is adding contentiously........ it has to add only one time whenever you call

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 Levike · Dec 19, 2015 at 01:04 PM 0
Share

There is no force on the ball only at Start() with a delay of 3 sec.

GetComponent ().velocity = 20;

The 1000 force is just explaining the bug. How the ball behaves. As soon as it touch the AI racket the first time it bounces back with a lot of speed. If I remove the $$anonymous$$ovePosition then everything is good except the AI racket is not moving.

avatar image Levike · Dec 19, 2015 at 01:08 PM 0
Share

Ohh you meant the second code. Yes but the velocity here is the position of the ball that needs to be updated every frame.

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

34 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

Related Questions

How to use Rigidbody2D.MovePosition to move a kinematic object around looping waypoints? 1 Answer

Rigidbody2D doesn't fall correctly when using MovePosition to move it horizontally 0 Answers

Move a RigidBody with MovePosition up and down 1 Answer

How do I prevent a rigidbody2D from falling through the ground? 0 Answers

Rigidbody2D.MovePosition acting a bit strange 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