how do i switch form wavepoint to a different state already got part to work but cant get the shooting state to work?
im trying to make it so it follows the wave point until it ether hits the end or focus target and if at end of wp it focuses a object but i cant seem to change the state to shooting any idea?
i have already got the pursue to work but i cant get the shooting state to work.
public class enemychaseshooting : MonoBehaviour { public Transform player; public Transform head; public Transform Plane; public Transform shipTarget;
string state = "patrol";
public GameObject[] waypoints;
int currentWP = 0;
public float rotSpeed = 0.2f;
public float speed = 1.5f;
float accuracyWP = 2.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
if (state == "patrol" && waypoints.Length > 0)
{
if (Vector3.Distance(waypoints[currentWP].transform.position, transform.position) < accuracyWP)
{
currentWP++;
if (currentWP >= 7)
{
state = "shooting";
}
}
/// rotate towards waypoint
direction = waypoints[currentWP].transform.position - transform.position;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), rotSpeed * Time.deltaTime);
this.transform.Translate(0, 0, Time.deltaTime * speed);
}
if (Vector3.Distance(player.position, this.transform.position) < 10 || state == "pursuing")
{
state = "pursuing";
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), rotSpeed * Time.deltaTime);
if (direction.magnitude > 5)
{
this.transform.Translate(0, 0, Time.deltaTime * speed);
}
}
else
{
state = "patrol";
}
**if ( state == " shooting")
{
if (shipTarget != null)
{
transform.LookAt(shipTarget);
}**
}
}
}
Answer by Matti-Jokipii · Feb 06, 2018 at 10:48 PM
The if-else in the middle leaves state either "pursuing" or "patrol". Perhaps you want this to be else if, not if?
i have gotten the pursuing to work not the shooting one
The if (state == "shooting") is never reached with state == "shooting" because you set:
state = "pursuing"
or
state = "patrol"
just before the
if (state == "shooting")
That is why you never have state == "shooting"
I was able to get the state to shooting but is it because its its the update? are you saying i should change it to 3 blocks if - if else - and if else again?
Your answer
Follow this Question
Related Questions
c# Coroutines and Waypoints HELP PLS!!!,C# Coroutine and Waypoints Help pls!!! 2 Answers
Can't find Animator State/Invalid Layer Index in child object inside prefab 0 Answers
How do i randomize certain waypoints at the end of a path? 0 Answers
How do i switch to a different state to target a certain thing? 0 Answers
How to resume waypoints after it has run into something and messed up? 0 Answers