for a space ship game, i’m trying to make it so when an ally collides with an enemy it recoils off the enemy but this error keeps coming up.
using UnityEngine;
using System.Collections;
public class allyAI : MonoBehaviour {
private GameObject aenemy;
private Transform aenemyTransform;
public float allySpeed;
public int allyhealth = 100;
private int shipCollideRecoil = 50;
// Use this for initialization
void Awake () {
}
// Update is called once per frame
void Update () {
aenemy = GameObject.FindWithTag("enemy");
aenemyTransform = aenemy.transform;
transform.position = Vector3.MoveTowards (transform.position, aenemy.transform.position, allySpeed);
transform.LookAt(aenemyTransform);
if (allyhealth < 0){
Destroy(this.gameObject);
}
}
void OnCollisionEnter (Collision collide){
if (collide.gameObject.tag == "enemy") {
SubAmount (10);
transform.position = new Vector3 (transform.position,transform.position + shipCollideRecoil, transform.position); //error appears here//
}
}
void SubAmount(int numtoSub){
allyhealth -= numtoSub;
}
}
also while i am at it i might as well ask, how do you make an object move towards another random object with the same tag “enemy”? because there is multiple enemies and the allies keep going after one ship.