- Home /
How do I get the sprite pointing the right direction?
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
Answer by Redbobster · Jan 13, 2017 at 08:32 AM
Hey guys, fixed the problem, just took a different approach to the rotation of the turret, all the calculations for leading the shot works and the AI is now laser accurate while you move in a straight line. Here's the code for those who are having similar issues or just need a code for leading shots:
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;
//values for internal use
private float distTarget;
private float velocityDist1;
private float velocityDist2;
private Vector3 velocityPosition1;
private Vector3 velocityPosition2;
private Vector3 TargetInterceptPosition;
Vector3 _prevPosition;
public void Start()
{
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;
if (Target)
{//allows the ai to lead shots rather than shooting at the targets current position
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));
Vector3 vectorToTarget = TargetInterceptPosition - transform.position;//finds out where the enemy is in relation to target
float angle = (Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg) - 90;//finds the angle the turret needs to turns towards
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * RotationSpeed);//turns the turret towards the target A.K.A player
}
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;
}
}