- Home /
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?
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?
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[i].transform.position - transform.position;
float dot = Vector3.Dot(heading, transform.forward);
if(Vector3.Distance(targets[i].transform.position, gameObject.transform.position) < 150.0f && !ReferenceEquals(targets[i], 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<AudioSource>().PlayOneShot(marbleHit, 1.0f);
}
}
And another piece that could be relevant:
private void initMarble()
{
marble = GameObject.CreatePrimitive(PrimitiveType.Sphere);
marble.name = "Marble";
marble.GetComponent<SphereCollider>().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<Renderer>().material.color = owner.transform.Find("Body").GetComponent<Renderer>().material.color;
marble.AddComponent<Rigidbody>();
marble.GetComponent<Rigidbody>().useGravity = false;
marble.transform.rotation = owner.transform.rotation;
marble.AddComponent<MarbleManager>();
marble.GetComponent<MarbleManager>().Owner = owner;
marble.AddComponent<AudioSource>();
}
[1]: /storage/temp/129501-img-2554.jpg
Answer by Vega4Life · Dec 16, 2018 at 12:45 AM
Here is the main code that determines if it hits something or not
Vector3 heading = targets[i].transform.position - transform.position;
float dot = Vector3.Dot(heading, transform.forward);
if(Vector3.Distance(targets[i].transform.position, gameObject.transform.position) < 150.0f && !ReferenceEquals(targets[i], 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.
Just for informational purposes. If you did want to have a system that was equivalent to your picture above - using angles. I would do it like this ins$$anonymous$$d of the dot product.
float los= 60f;
float range = 100f;
Vector3 heading = enemy.position - transform.position;
float distance = heading.magnitude;
if (Vector3.Angle(heading, transform.forward) <= los && distance <= range)
{
// Is a target
}
If target is out front within our 60 degree line of sight and within our targeting range of 100f, then we can hit it.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
OnCollisionEnter Logic Error 1 Answer
I want a better explination than the one in unitys tutorial 2 Answers
Alarm lights not changing 1 Answer