Ball hitting pivot Help

My Ball keeps hitting the pivot and not going straight through it… I’m not sure what I did wrong here. I think it is a setting instead of coding…

Here is my code too just in case:

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

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

private Rigidbody2D currentBallRigidbody;
private SpringJoint2D currentBallSprintJoint;

private Camera mainCamera;
private bool isDragging;

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

    SpawnNewBall();
}

// Update is called once per frame
void Update()
{
    if (currentBallRigidbody == null) { return;}

    if (!Touchscreen.current.primaryTouch.press.isPressed)
    {
        if (isDragging)
        {
            LaunchBall();
        }

        isDragging = false;

        return;
    }

    isDragging = true;
    currentBallRigidbody.isKinematic = true;

    Vector2 touchPosition = Touchscreen.current.primaryTouch.position.ReadValue();

    Vector3 worldPosition = mainCamera.ScreenToWorldPoint(touchPosition);

    currentBallRigidbody.position = worldPosition;
}

private void SpawnNewBall()
{
    GameObject ballInstance = Instantiate(ballPrefab, pivot.position, Quaternion.identity);

    currentBallRigidbody = ballInstance.GetComponent<Rigidbody2D>();
    currentBallSprintJoint = ballInstance.GetComponent<SpringJoint2D>();

    currentBallSprintJoint.connectedBody = pivot;
}

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

    Invoke(nameof(DetachBall), detachDelay);
}

private void DetachBall()
{
    currentBallSprintJoint.enabled = false;
    currentBallSprintJoint = null;

    Invoke(nameof(SpawnNewBall), respawnDelay);
}

}

It might be that it’s hitting the ball that is left in the scene. The Scene file should have no ball, as the Ball Handler’s job is to put one in the scene when it’s played… You might also increase the time before the ball respawns (respawn Delay)

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

Privacy & Terms