• 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 LyleD · Jun 11, 2015 at 10:33 AM · movementtransformparent

Ball Cont. Moving Despite transform.parent Stopping

I have a Ball which is controlled by the Paddle's transform when the Ball is on the Paddle. However, when the Paddle hits an object (Enemy block), it should stop movement completely in the direction it was traveling, and the Ball should stop completely too (it's transform.parent is set to null when Paddle hits Enemy). Currently, the Ball continues moving incrementally when the Paddle hits the Enemy.

alt text

Ball Code

 using UnityEngine;
 using System.Collections;
 
 public class BallMove : MonoBehaviour {
 
     private Rigidbody2D rb;
     public bool ballOnPaddle;
 
     public float ballVelocity;
     public GameObject paddle;
 
 
     // Awake is created when the ball is created. This will work when respawning the ball.
     void Awake () 
     {
         rb = GetComponent<Rigidbody2D> ();
     }
 
     // Use this for initialization
     void Start () {
         ballVelocity = 1200;
     }
 
     // Ball is more reactive in Update instead of FixedUpdate
     void Update()
     {
         if ((Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.RightArrow)) && Input.GetKeyDown (KeyCode.Space) && ballOnPaddle == true) {
             
             transform.parent = null;
             ballOnPaddle = false;
             
             rb.AddForce (new Vector2(ballVelocity,ballVelocity));
         }
         
         if ((Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.LeftArrow)) && Input.GetKeyDown (KeyCode.Space) && ballOnPaddle == true) {
             
             transform.parent = null;
             ballOnPaddle = false;
             
             rb.AddForce (new Vector2(-ballVelocity,ballVelocity));
         }
         
         if (Input.GetKeyDown("space") && ballOnPaddle == true) {
             transform.parent = null;
             ballOnPaddle = false;
             
             rb.AddForce (new Vector2(0,ballVelocity));
         }
     }
 
     void OnCollisionEnter2D(Collision2D coll)
     {
 
         if (coll.gameObject.tag == "Paddle") {
             ballOnPaddle = true;
             rb.velocity = Vector3.zero;
         }
     }
 
     void OnCollisionExit2D(Collision2D exit)
     {
         if (exit.gameObject.tag == "Paddle") {
             ballOnPaddle = false;
         }
     }
 
 }

Paddle Code

 using UnityEngine;
 using System.Collections;
 
 public class PaddleMove : MonoBehaviour {
 
     private bool canMoveLeft = true;
     private bool canMoveRight = true;
     private float paddleSpeed = 25;
     private Vector2 contactPoint;
     private Vector2 lContact;
     private Vector2 rContact;
     private Vector3 paddlePosition;
 
     //Ball GameObject components
     public GameObject ball;
     private BallMove ballMove;
     private Rigidbody2D ballRB;
 
     void Awake () {
         ballMove = ball.GetComponent<BallMove> ();
         ballRB = ball.GetComponent<Rigidbody2D> ();
     }
 
     // Use this for initialization
     void Start () {
         lContact = new Vector2(1,0);
         rContact = new Vector2 (-1, 0);
     }
     
     void FixedUpdate () {
 
         if (ballMove.ballOnPaddle == true && (canMoveLeft && canMoveRight)) {
             ball.transform.parent = gameObject.transform;
         } else {
             ball.transform.parent = null;
         }
 
         if (canMoveLeft && canMoveRight) {
             // Get any changes from player input
             float xPos = gameObject.transform.position.x + (Input.GetAxis ("Horizontal") * paddleSpeed * Time.deltaTime);
             // Set a clamped player position with xPos
             paddlePosition = new Vector3 (Mathf.Clamp (xPos, -24, 24), -12, 0);
             // Update the paddle's position
             gameObject.transform.position = paddlePosition;
         } else { // The else keeps the ball from moving on the y-axis
             float xPos = gameObject.transform.position.x;
             paddlePosition = new Vector3 (Mathf.Clamp (xPos, -24, 24), -12, 0);
             gameObject.transform.position = paddlePosition;
         }
 
         if ((canMoveLeft == false) && (Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.RightArrow))) {
             float xPos = gameObject.transform.position.x + (Input.GetAxis ("Horizontal") * paddleSpeed * Time.deltaTime);
             paddlePosition = new Vector3 (Mathf.Clamp (xPos, -24, 24), -12, 0);
             gameObject.transform.position = paddlePosition;
             ball.transform.parent = gameObject.transform;
         }
 
         if ((canMoveRight == false) && (Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.RightArrow))) {
             float xPos = gameObject.transform.position.x + (Input.GetAxis ("Horizontal") * paddleSpeed * Time.deltaTime);
             paddlePosition = new Vector3 (Mathf.Clamp (xPos, -24, 24), -12, 0);
             gameObject.transform.position = paddlePosition;
             ball.transform.parent = gameObject.transform;
         }
 
 
     }
     
     void OnCollisionEnter2D(Collision2D coll)
     {
 
         if (coll.gameObject.tag == "Enemy") {
                         
             // Get contact point from Paddle
             ContactPoint2D contact = coll.contacts[0];
             contactPoint = contact.normal;
             
             // If Paddle hits on its left side...
             if (contactPoint == lContact) {
                 canMoveLeft = false;
             }
             
             // If Paddle hits on its right side...
             if (contactPoint == rContact) {
                 canMoveRight = false;
             }
         }
 
         if (coll.gameObject.tag == "Enemy" && ballMove.ballOnPaddle == true) {
 //            ball.transform.parent = null;
         }
     }
 
     void OnCollisionExit2D(Collision2D coll)
     {
         if (coll.gameObject.tag == "Enemy") {
             canMoveLeft = true;
             canMoveRight = true;
         }
     }
 
     // Add:boolean to check if ballIsOnPaddle before oncollisionexit affects the ball at all
 
 } // End of main





unity.gif (250.8 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Making child/parent movement smooth instead of choppy 1 Answer

moving child objects in hierarchy and keep stacks/sorting? Help needed 0 Answers

Rigidbody Moveposition not working when Camera follows object? 0 Answers

all parents of a transform 1 Answer

Setting parent of instantiated object fails (Error: setting parent of prefab is disabled...) 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