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