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;
}
}