Hello, you wonderful people I'm new here??. I wanted to ask for assistance, right now I am creating a Billy Hatcher/Katamari Damacy clone in unity. I've made strides in development and was able to create a script whenever the player collides with a sphere, to able to roll the sphere. While there are some hiccups, I'm asking if there is any way to make this system better?
Script implementation:
public class AttachToGum : MonoBehaviour
{
//For Attaching with Gumball
private GameObject[] gumBalls;
private Transform playerTransform;
private Transform player;
float distacne = Mathf.Infinity;
public float attachRequireDistance = 4f;
public GameObject gum;
public GameObject GumControl;
GumballMovement mov;
Rigidbody hitRB;
Rigidbody forGum;
public Vector3 offset = new Vector3(0, 1, 0);
public LayerMask pickupMask;
bool isAttached = false;
// Start is called before the first frame update
void Start()
{
gumBalls = GameObject.FindGameObjectsWithTag("GumBall");
player = GameObject.FindWithTag("Player").transform;
player.gameObject.layer = LayerMask.NameToLayer("BallDetatch");
mov = FindObjectOfType<GumballMovement>();
}
// Update is called once per frame
void Update()
{
TouchingGum();
}
private void TouchingGum()
{
foreach (GameObject gumball in gumBalls)
{
RaycastHit hit = new RaycastHit();
distacne = Vector3.Distance(gumball.transform.position, player.transform.position);
CharacterController DetectCollisions = gumball.GetComponent<CharacterController>();
//Debug.Log(gumball);
if (distacne < attachRequireDistance)
{
if (Input.GetButtonDown("Attach"))
{
isAttached = true;
}
}
else
{
isAttached = false;
}
if (isAttached)
{
//DetectCollisions.enabled = true;
hitRB = gumball.gameObject.GetComponent<Rigidbody>();
Vector3 heldOffset = (transform.right * offset.x) + (transform.forward * offset.z) + (transform.up * offset.y);
hitRB.isKinematic = true;
//gumball.GetComponent<SphereCollider>().enabled = false;
//player.GetComponent<CharacterController>().center = new Vector3(offset.x, offset.y, offset.z);
//player.GetComponent<CharacterController>().radius = 3.0f;
//hit.collider.gameObject.transform.position = GumControl.transform.position;
//hit.collider.gameObject.transform.position = player.transform.position + heldOffset;
//player.transform.position = gumball.transform.position + heldOffset;
hitRB.MovePosition(GumControl.transform.position);
hitRB.MoveRotation(GumControl.transform.rotation);
//Vector3 gumVector = GumControl.transform.position - gumball.transform.position;
//forGum.AddForce(gumVector * 1);
}
else if (!isAttached && !hitRB == null)
{
hitRB.isKinematic = false;
}
}
}
}
Implementation reference:
(from 2:08-2:22): https://www.youtube.com/watch?v=ncQmmeLz8xI