Enemy is shooting the opposite direction of player
Hello everyone, as the question says I am having an issue with my Grunt enemy class. It shoots its bullets when the player is in range of the raycast. The problem is it is shooting opposite from the player XD I want it to shoot in the direction of the player. Below are the scripts to make the grunt work. I have been looking around online and I couldn't find a solid solution. Thank you in advance!
Kindly,
Harpoaian
Grunt Bullet Data Model:
Grunt theGrunt;
Vector3 pos;
Vector3 bulletvelocity;
// Use this for initialization
void Start()
{
theGrunt = GameObject.Find("Grunt").GetComponent<Grunt>();
bulletvelocity = new Vector3(bulletSpeed * Time.deltaTime, 0, 0);
pos = transform.position;
theGrunt = GetComponent<Grunt>();
if (GameObject.Find("Grunt").GetComponent<GruntStateMachine>().colliding)
{
bulletvelocity = new Vector3(bulletSpeed * 1 * Time.deltaTime, 0, 0);
//transform.position += transform.right * Time.deltaTime * bulletSpeed;
}
else
{
bulletvelocity = new Vector3(bulletSpeed * -1 * Time.deltaTime, 0, 0);
//transform.position -= transform.right * Time.deltaTime * bulletSpeed;
//Debug.Log("I should shoot left");
}
}
// Update is called once per frame
void Update()
{
pos += bulletvelocity;
transform.position = pos;
// transform.position += transform.right * Time.deltaTime * bulletSpeed;
}
Grunt Statemachine:
void Vision()
{
Debug.DrawLine(sightStart.position, thePlayer.position, Color.red);
spotted = Physics2D.Linecast(sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer("Player"));
if(spotted == true)
{
SetState(GruntStates.FIRE);
Debug.Log("I should fire!");
}
else
{
SetState(GruntStates.PATROL);
}
}
void Patrol()
{
//enemy movement goes here
GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
colliding = Physics2D.Linecast(sightStart.position, wallEnd.position, detectWhat);
if(colliding == true)
{
transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
moveSpeed *= -1;
}
Debug.DrawLine(sightStart.position, wallEnd.position, Color.green);
}
void FiringState()
{
trackTime += Time.deltaTime;
Vision();
Debug.Log("In firing state!");
//transform.LookAt(thePlayer);
if(trackTime >=2)
{
Instantiate(bullet, transform.position, transform.rotation);
//Instantiate(bullet, sightStart.position, thePlayer.transform);
trackTime = 0;
}
}
Answer by LSPressWorks · Jul 27, 2017 at 10:29 PM
using *-1 reverses the direction of stuff
bulletvelocity = new Vector3(bulletSpeed * -1 * Time.deltaTime, 0, 0);
has that exact effect.
try making that not say -1 and let us know
That didn't work it just made my grunts bullets go right. I have discovered something though. $$anonymous$$y Grunt does work as intended BUT he isn't consistent. Would I be able to make the projectile instantiate and go the same direction that my raycast is facing?
I figured out the answer. Here is the code that I used. Basically I called the public variable facingRight in my Grunt State $$anonymous$$achine. $$anonymous$$y Grunt now shoots the correct direction that it's facing and will only fire when the player is in front of it.
if (GameObject.Find("Grunt").GetComponent().facingRight == true)
{
bulletvelocity = new Vector3(bulletSpeed * 1 * Time.deltaTime, 0, 0);
//transform.position += transform.right * Time.deltaTime * bulletSpeed;
Debug.Log("I should shoot right");
}
else if (GameObject.Find("Grunt").GetComponent<GruntState$$anonymous$$achine>().facingRight == false)
{
bulletvelocity = new Vector3(bulletSpeed * -1 * Time.deltaTime, 0, 0);
//transform.position -= transform.right * Time.deltaTime * bulletSpeed;
Debug.Log("I should shoot left");
}