Explanation as to how this game mechanic works?

I’m still very C# illiterate, and I want to know what these two scripts do exactly. Scroll down to see the scripts.

The game I’m looking at is basically a racing game. The player and AI can use this item called ‘Marble’, which can be thrown from their car and be targeted towards an opponent that’s in-front of it. This causes the targeted car to slow down and spin out of control for a couple seconds.

If there’s no opponent in front, the marble just gets thrown straight ahead.

My question are as follows:

  • How does this script chose which target to pick? Using my badly drawn drawing as an example, I’m assuming the marble would target the nearest opponent from the player?

[129502-gsddsd.png*_|129502]

  • Am I right in saying that if the marble doesn’t end up hitting any target, it only exists in the scene for 8 seconds?

  • Is there a range limit as to how much the marble can angle itself towards an opponent? Using this example here below, if there was a range limit, I’d assume that the marble wouldn’t be targeted towards the opponent car outside the angle range, and instead just move straight ahead?

[129501-img-2554.jpg*_|129501]

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

public class MarbleManager : MonoBehaviour {

    private const float MAXSPEED = 250f;

    private int targetIndex;
    private GameObject owner;
    private GameObject[] targets;
    private float lifeTimer;
    private const float MAXTIME = 8.0f;
    private AudioClip marbleHit;

    public GameObject Owner { get { return owner; } set { owner = value; } }

	void Start () {
        targetIndex = -1;

        lifeTimer = 0.0f;

        gameObject.GetComponent<Rigidbody>().velocity = gameObject.transform.forward * MAXSPEED;

        targets = GameObject.FindGameObjectsWithTag("Player");

        marbleHit = Resources.Load<AudioClip>("Sounds/KartEffects/marbleCollision");
    }
	
	void Update () {
        
        if (targetIndex != -1)
        {
            transform.LookAt(targets[targetIndex].transform);
            gameObject.GetComponent<Rigidbody>().velocity = gameObject.transform.forward * MAXSPEED;
        }
        else
        {
            for(int i = 0; i < targets.Length && targetIndex == -1; i++)
            {
                Vector3 heading = targets*.transform.position - transform.position;*

float dot = Vector3.Dot(heading, transform.forward);
if(Vector3.Distance(targets_.transform.position, gameObject.transform.position) < 150.0f && !ReferenceEquals(targets*, owner) && dot > 0)
{
targetIndex = i;
}
}
}*_

lifeTimer += Time.deltaTime;
if(lifeTimer >= MAXTIME)
{
Destroy(gameObject);
}
}

void OnTriggerEnter(Collider other)
{

if (other.gameObject.tag.Contains(“Track”))
{
playCollisionEffect();
Destroy(gameObject);
}
else if (other.gameObject.tag.Contains(“Player”) && !ReferenceEquals(other.gameObject, owner))
{
playCollisionEffect();
}

}

public bool validTarget(GameObject target)
{
return !ReferenceEquals(target, owner);
}

private void playCollisionEffect()
{
gameObject.GetComponent().PlayOneShot(marbleHit, 1.0f);
}
}
And another piece that could be relevant:
private void initMarble()
{
marble = GameObject.CreatePrimitive(PrimitiveType.Sphere);
marble.name = “Marble”;
marble.GetComponent().isTrigger = true;
marble.transform.localScale = new Vector3(2.0f, 2.0f, 2.0f);
marble.transform.position = new Vector3(owner.transform.position.x, owner.transform.position.y, owner.transform.position.z);
marble.GetComponentInChildren().material.color = owner.transform.Find(“Body”).GetComponent().material.color;
marble.AddComponent();
marble.GetComponent().useGravity = false;
marble.transform.rotation = owner.transform.rotation;

marble.AddComponent();
marble.GetComponent().Owner = owner;

marble.AddComponent();
}

*
*
_*

Here is the main code that determines if it hits something or not


                 Vector3 heading = targets*.transform.position - transform.position;*

float dot = Vector3.Dot(heading, transform.forward);
if(Vector3.Distance(targets_.transform.position, gameObject.transform.position) < 150.0f && !ReferenceEquals(targets*, owner) && dot > 0)
----------
The dot product is just the projection of one vector of the other. In this case, its just used to determine if the target is in front of our gameObject. Interestingly, it does just dot > 0. This basically means as long as its in front, regardless of angle, its good to go. Then there is the distance check of within 150f meters.
----------
So pretty much if the target is in front (regardless of angle) and is within 150f meters, it is a valid target.
----------*_