0
\$\begingroup\$

enter image description here

I am trying to place the ball on the paddle until a key is pressed, also there is box collider that triggers if the ball falls down. For some reason the transform.position.y of the ball gameObject is going below zero while the ball is still stuck to the paddle in the game and therefore triggering the collider unnecessarily . Attaching code of paddle and ball bellow .

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ballScript : MonoBehaviour
{
    [SerializeField] GameObject myPaddle;
    Vector3 paddleToBallVector;
    void Start()
    {
        paddleToBallVector = transform.position - myPaddle.transform.position;
        lockToPaddle();
    }
    void Update()
    {
        lockToPaddle();
    }

    private void lockToPaddle()
    {
        transform.position = myPaddle.transform.position + paddleToBallVector;
    }
}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class paddleScript : MonoBehaviour
{
    [SerializeField] float screenWidthUnits = 16f;
    [SerializeField] float paddleLeftLimit = 1.07f;
    [SerializeField] float paddleRightLimit = 14.928f;

    void Update()
    {
        float screenWidthPercentage = Input.mousePosition.x / Screen.width;
        Vector2 curPaddlePos = new Vector2(screenWidthPercentage * screenWidthUnits, transform.position.y);
        curPaddlePos.x = Mathf.Clamp(curPaddlePos.x, paddleLeftLimit, paddleRightLimit);
        transform.position = curPaddlePos;
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class bottomCollisionScript : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log(collision.gameObject);
        Debug.Log("Game Over");
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ I have also added my Triggering Box collider 2D's Script and screenshot as well \$\endgroup\$
    – Kevin Tony
    Apr 25, 2020 at 17:13
  • \$\begingroup\$ Can you confirm whether your ball has a rigidbody on it, or any other components of note? How are your ball's components configured? \$\endgroup\$
    – DMGregory
    Apr 25, 2020 at 18:09
  • \$\begingroup\$ @DMGregory Sorry for the late reply, My ball had a RigidBody2D component with Body type Dynamic everything else at default settings.It was locked to above my paddle by updating its position each frame. At this setting is it possible that the gravity on the ball was increasing its velocity over time and eventually causing the ball to reach lower and lower in between each frame update. I assume so because when i really slowed my game i could see an glimpse of my ball that kept appearing lower and lower , also setting the Body type to Kinematic fixed my issue \$\endgroup\$
    – Kevin Tony
    May 7, 2020 at 18:38
  • \$\begingroup\$ If you've solved your problem, please post your solution as an answer. This is also a good lesson to never position a rigidbody using its transform, lest your displayed position depart from or mask what's really going on in the underlying physics. \$\endgroup\$
    – DMGregory
    May 7, 2020 at 18:51

2 Answers 2

1
\$\begingroup\$

I found what i did wrong , It was because I used transform to position a Object that had a rigid body. The RigidBody component causes the object to be subject to Physics Laws and trying to reposition it using transform didn't stop factor like gravity acting on it.

\$\endgroup\$
0
\$\begingroup\$

In the ball script you need to call

paddleToBallVector = transform.position - myPaddle.transform.position;

in the Update method and not in Start, before you call lockToPaddle. That is because the paddle might be moving in the mean time.

Should note that this is not the proper solution as this can cause the ball to get inside the collider of the paddle. Instead you should you should use a RigidBody2D and apply a force.

\$\endgroup\$
2
  • \$\begingroup\$ First of all thank you for your answer. I tried your solution and now the ball wont stay fixed on the paddle.What confuses me is that the paddle would never change its value in the y axis as its movement is only in the x direction, so how did my ball get a position.transform.y value below 0.Could you please share a better solution \$\endgroup\$
    – Kevin Tony
    Apr 25, 2020 at 17:04
  • \$\begingroup\$ @KevinTony In your case if you want to keep the collider as trigger, add a bool that indicates whether the ball has collided with the paddle. If you detect the collision then set it to true and stop moving the ball in the Update method. A logical thing to do then would be to set the position of the ball to a fixed position relative to the paddle so that it locks on a certain position \$\endgroup\$ Apr 25, 2020 at 22:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .