This is my first ever c# script. I have played around a little and I think this is some proper c# code. Since it's my very first c# ever, I'm assuming there's a few mistakes, things that could be better, bad conventions and such. If you see any, please let me know so I can improve!
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Mover : MonoBehaviour
{
[SerializeField] GameObject target;
private GameObject player; // creating a variable for the player object
void Update()
{
if (target != null) { // if the target is already destroyed, we don't have to do anything anymore
player = this.gameObject; // the script is attached to the player
// Here I expected I could get the NavMeshAgent component via something like player.NavMashAgant.
// I do the same later for target.transform.
// Is there a reason this component is not accessible via dot notation?
GetComponent<NavMeshAgent>().destination = target.transform.position; // set the players destination as the targets potion. It starts moving automatically.
if (AreClose(player, target)) {
Destroy(target);
}
}
}
private static double Distance(GameObject a, GameObject b)
{
Vector3 aPos = a.transform.position;
Vector3 bPos = b.transform.position;
// A really long line.. What is the convention to make this more readable?
return Math.Sqrt(Math.Pow(aPos.x - bPos.x, 2) + Math.Pow(aPos.y - bPos.y, 2) + Math.Pow(aPos.z - bPos.z, 2));
}
private static bool AreClose(GameObject a, GameObject b)
{
double result = Distance(a, b);
if (result < 2.5) // random magic number for now
{
return true;
} else
{
return false;
}
}
}