[SOLVED] Ball bounces from pivot

I went through the whole first course in Udemy for doing mobile games with Unity, and the only issue I had was that for some weird reason the ball bounces back from the pivot point/object after it gets launched. Any idea what could be the reason?

The ball prefab has “Order in layer” set to 1, while the pivot has “Order in layer” set to 0. Doesn’t that mean that they shouldn’t contact with each other?

Any help will be appreciated :slight_smile:

Hi,

It sounds like that the detachment code is not working and so its still connected to the pivot rather than releasing as we had this in a previous lecture.
I would compare your code to that in the course using the Project Changes link in the resources to rule out an issue there.

If you cant find it please post your ball launcher code here and we can take a look :slight_smile:

Thank you for the quick reply.

The links in the course (in Udemy) don’t work - so I had to find the repo in other way and then compare. I can’t find any problematic differences between the code. I think it could be some setting of the materials, so here are screenshots of both the pivot and the ball prefab settings:

And also - here is the code in Assets/Scripts/BallHandler.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.EnhancedTouch;
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;

public class BallHandler : MonoBehaviour
{
    [SerializeField] private GameObject ballPrefab;
    [SerializeField] private Rigidbody2D pivot;
    [SerializeField] private float respawnDelay;
    [SerializeField] private float delayBeforeDetachBall;

    private Rigidbody2D currentBallRigidbody;
    private SpringJoint2D currentBallSpringJoint;
    private Camera mainCamera;
    private bool isDragging;

    // Start is called before the first frame update
    void Start()
    {
        mainCamera = Camera.main;
        SpawnNewBall();
    }

    void OnEnable() {
        EnhancedTouchSupport.Enable();
    }

    void OnDisable() {
        EnhancedTouchSupport.Disable();
    }

    // Update is called once per frame
    void Update()
    {
        if (currentBallRigidbody == null) {
            return;
        }
        if (Touch.activeTouches.Count == 0) {
            if (isDragging) {
                LaunchBall();
            }
            isDragging = false;
            return;
        }
        isDragging = true;
        currentBallRigidbody.isKinematic = true;

        Vector2 touchPosition = new Vector2();

        foreach(Touch touch in Touch.activeTouches)
        {
            touchPosition += touch.screenPosition;
        }
        touchPosition /= Touch.activeTouches.Count;

        Vector3 worldPosition = mainCamera.ScreenToWorldPoint(touchPosition);
        // Debug.Log(worldPosition);
        currentBallRigidbody.position = worldPosition;
    }

    private void SpawnNewBall()
    {
        GameObject ballInstance = Instantiate(ballPrefab, pivot.position, Quaternion.identity);
        currentBallRigidbody = ballInstance.GetComponent<Rigidbody2D>();
        currentBallSpringJoint = ballInstance.GetComponent<SpringJoint2D>();
        currentBallSpringJoint.connectedBody = pivot;
    }

    private void LaunchBall()
    {
        currentBallRigidbody.isKinematic = false;
        currentBallRigidbody = null;

        Invoke(nameof(DetachBall), delayBeforeDetachBall);
    }
    
    private void DetachBall()
    {
        currentBallSpringJoint.enabled = false;
        currentBallSpringJoint = null;
        Invoke(nameof(SpawnNewBall), respawnDelay);
    }
}

=====

And once again - thank you for your help :slight_smile:

Hi Pety,

Thanks for that information.
I have checked the links on udemy and they appear to be working for me to get to the changes page on gitlab.

That aside the issue is because you have a box collider 2D on your pivot.

I would remove that and retest as i believe it should all work correctly then.

If you are having issues after this please do let me know and i will pick it up in the morning :slight_smile: (I checked in as was at the pc and my phone notified me)

Hey, Marc,

Thank you for the help - I unchecked the box colider 2D checkbox - and now all works as expected:

However, on the video tutorials - I can’t really understand that the collider 2d is turned off or disabled:

(I’m on Linux as the video looks to be shot on Windows.)

And one last thing that I don’t get: the balls and the pivot have different “Order in layer” … isn’t that sufficient so that they do not “collide” with each other? :slight_smile:


In any case - thank you so much for the quick responses - and for the help :bowing_man:

Hi Petyr,

I think that the two questions link into the same answer here.
Order Layers are on the sprite renderer and so that its the order in which they render.
The reason we changed ours although i didnt really experience this visually is because the ball was rendering behind the pivot and giving Nathan an odd shading issue.
Order layers wont ignore collisions because we would need to tell the physics system to ignore the collisions say on the ball from anything on the order layer for the pivot.

Its much easier to just remove the collider as we are not using it in either case.

Hope this explains :slight_smile:

Thank you very much for the good explanation, Marc :slight_smile:
I’ll mark this thread as “Solved”.


And one last tiny bit of information - I checked the links on Udemy with Google Chrome - and they work there. So guess there is issue with them only on Firefox - but of course, this is a problem of udemy.com itself :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms