- Home /
Question by
smitrajrana57 · Aug 02, 2020 at 01:12 PM ·
scripting problemmovementscripting beginner
How to stop an object at a specific position for certain time in it's motion and then let continue it's motion?
I want to create an enemy AI which continuously moves along y-axis in fixed range.I want it to stop at it's upper limit of range and instantiate poison(other game object) .Though I have written a code to do it, The instantiation of poison and stopping of enemy happens haphazardly.What change I should make to the code? { public float speed; public Rigidbody2D rb; Vector2 upperlimit; Vector2 lowerlimit; public int range;
//poisoning
public ArrayList array;
public float nextActionTime = 4f;
public float period = 2f;
public GameObject posion;
public Transform emitPoint;
public float InstantiationTimer = 2f;
void Start()
{
range = range / 2;
upperlimit = new Vector2(transform.position.x + range, transform.position.y + range);
lowerlimit = new Vector2(transform.position.x - range, transform.position.y - range);
rb.velocity = new Vector2(0, speed);
}
// Update is called once per frame
void Update()
{
Debug.Log(Time.time);
if (transform.position.y > upperlimit.y)
{
transform.position = new Vector2(transform.position.x, upperlimit.y);
CreatePoison();
if (Time.time > nextActionTime)
{
rb.velocity = new Vector2(0, -rb.velocity.y);
nextActionTime += period;
}
}
else if (transform.position.y < lowerlimit.y)
{
transform.position = new Vector2(transform.position.x, lowerlimit.y);
rb.velocity = new Vector2(0, -rb.velocity.y);
}
}
void CreatePoison()
{
InstantiationTimer -= Time.deltaTime;
if (InstantiationTimer <= 0)
{
Instantiate(posion, emitPoint.position, posion.transform.rotation);
InstantiationTimer = 2f;
}
}
Comment