- Home /
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.
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
Your answer
Follow this Question
Related Questions
Making child/parent movement smooth instead of choppy 1 Answer
Random Movement? 1 Answer
Rigidbody Moveposition not working when Camera follows object? 0 Answers
How to instantiate a GameObject in a scene and keep world transform? 3 Answers
Getting instance of an sub object rather than the original's subobject 0 Answers