This is what I have so far:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Targetting : MonoBehaviour {
public List<Transform> targets;
public Transform selectedTarget;
private Transform myTransform;
// Use this for initialization
void Start () {
targets = new List<Transform>();
selectedTarget = null;
myTransform = transform;
}
private void Kosketus()
{
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Enemy")
{
//stuff
targets.Add(collision.gameObject.transform);
}
}
}
private void SortTargetsByDistance()
{
targets.Sort(delegate(Transform t1, Transform t2){
return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
});
}
private void TargetEnemy()
{
if (selectedTarget == null) {
SortTargetsByDistance ();
selectedTarget = targets [0];
}
else {
int index = targets.IndexOf(selectedTarget);
if(index < targets.Count - 1)
{
index++;
}
else {
index = 0;
}
deselectTarget();
selectedTarget = targets[index];
}
selectTarget ();
}
private void selectTarget()
{
selectedTarget.renderer.material.color = Color.red;
PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");
pa.target = selectedTarget.gameObject;
}
private void deselectTarget()
{
selectedTarget.renderer.material.color = Color.blue;
selectedTarget = null;
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0)) {
Kosketus();
TargetEnemy ();
}
}
}
I have attached a box collider to Player’s front and I want each “Enemy” colliding with it to be added to list when mouse1 is pressed.
After that it should sort the list by distance from target and target the first in the list.
Now I am getting error: Targetting.cs(21,37): error CS1547: Keyword ‘void’ cannot be used in this context.
So, can someone tell me how I can use the OnCollisionEnter like in the script but without getting that error and if I am even remotely close what I am trying to achieve?
I am really new to unity/c# so I am not sure what I am doing and this is a targetting script made by someone other and I am trying to edit it.