I am busy making a 2D tank game and it was coming along nicely until I hit a road block, I’ve completed a line of code for my enemy AI which allows them to lead their shots, this line of code works but the sprite is turned on its side so you can’t it or the bullets it creates, here is the code:
using UnityEngine;
using System.Collections;
public class Enemy_Turret : MonoBehaviour {
//values that will be set in the Inspector
public GameObject Target;
public float RotationSpeed;
public float reload;
public float ReloadTime;
public GameObject Bullet;
public float projectileSpeed;
public float turnSpeed;
//values for internal use
private float distTarget;
private float velocityDist1;
private float velocityDist2;
private Vector3 velocityPosition1;
private Vector3 velocityPosition2;
private Vector3 TargetInterceptPosition;
private Quaternion rotationTarget;
Vector3 _prevPosition;
public void Start()
{
transform.rotation = Quaternion.AngleAxis(90f,Vector3.up);
Target = GameObject.FindWithTag("Friendly");
}
// Update is called once per frame
void FixedUpdate()
{
//finds velocity of the target
Vector3 vel = (Target.transform.position - _prevPosition) / Time.deltaTime;
_prevPosition = Target.transform.position;
//allows the ai to lead shots rather than shooting at the targets current position
if (Target)
{
distTarget = Vector3.Distance(Target.transform.position, transform.position);
velocityPosition1 = Target.transform.position + ((vel) * (distTarget / projectileSpeed));
velocityDist1 = Vector3.Distance(velocityPosition1, transform.position);
velocityPosition2 = Target.transform.position + ((vel) * (velocityDist1 / projectileSpeed));
velocityDist2 = Vector3.Distance(velocityPosition2, transform.position);
TargetInterceptPosition = Target.transform.position + ((vel) * (velocityDist2 / projectileSpeed));
rotationTarget = Quaternion.LookRotation((TargetInterceptPosition) - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(rotationTarget.eulerAngles.x + 90, rotationTarget.eulerAngles.y, rotationTarget.eulerAngles.z), Time.deltaTime * RotationSpeed);
Debug.Log(Target);
}
else
{
Target = GameObject.FindWithTag("Friendly");//or find closest object in seperate function, etc.
}
if (reload < 0)
{
Instantiate(Bullet, transform.position, transform.rotation);
reload = ReloadTime;
}
reload = reload - 1;
}
}
if you could help me fix this I would be very thankful, I’ve been scouring the internet for the past week and trying different solutions but nothing has worked