hi guys
I really thought i would be able to do this, but i been battling.
trying to get these two lines:
Vector2 randomDir = new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f));
rb.MovePosition(rb.position + randomDir * speed * Time.fixedDeltaTime);
to move my object in a random direction for a random time.
i know what has to happen.
choose a random direction, then keep going for x amount of time, rinse and repeat.
but no matter what im trying my object just doesnt move.
but i feel like im missing a process here on how to keep my object moving.
any help appreciated
here is my code so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RoundUFO : MonoBehaviour
{
public Rigidbody2D rb;
public Vector2 direction;
public float speed;
public Transform player;
public GameObject roundUFOExplodes;
public GameObject roundUFOTorpedo;
public float shootingDelay; //time between shots in seconds
public float lastTimeShot = 0;
public float torpedoSpeed;
public int shields = 10;
private float randomDistance;
private float dist;
public GameObject laserHits;
public int points;
private bool followingPlayer = false;
private float nextUpdateAt = -999f;
private bool moveRandomly;
private Vector2 randomDir;
// Start is called before the first frame update
void Start()
{
player = GameObject.FindWithTag ("Player").transform;
randomDistance = Random.Range(5, 20);
}
// Update is called once per frame
void Update()
{
Shooting();
}
private void FixedUpdate()
{
Vector2 randomDir = new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f));
if (followingPlayer)
{
//Debug.Log("Follow");
//figure out which way to move to approach the player
direction = (player.position - transform.position).normalized;
rb.MovePosition(rb.position + direction * speed * Time.fixedDeltaTime);
}
else
{
//Debug.Log("patrol");
if (Time.time > nextUpdateAt)
{
nextUpdateAt = Time.time + Random.Range(1f, 5f);
rb.MovePosition(rb.position + randomDir * speed * Time.fixedDeltaTime);
}
}
}
private void OnCollisionEnter2D(Collision2D collider)
{
if (collider.gameObject.tag == "Player")
{
DestroyRoundUFO();
}
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "LaserRed")
{
FindObjectOfType<AudioManager>().Play("LaserHit");
Instantiate(laserHits, transform.position, transform.rotation);
Destroy(collision.gameObject);
shields--;
if (shields <= 0)
{
DestroyRoundUFO();
}
}
}
public void DestroyRoundUFO()
{
player.SendMessage("ScorePoints", points);
FindObjectOfType<AudioManager>().Play("PlayerShipExplodes");
Instantiate(roundUFOExplodes, transform.position, transform.rotation);
Destroy(gameObject);
}
public void Shooting()
{
dist = Vector3.Distance(player.transform.position, gameObject.transform.position);
if (dist < randomDistance)
{
followingPlayer = true;
if (Time.time > lastTimeShot + shootingDelay)
{
lastTimeShot = Time.time;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg - 90f;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
GameObject newBullet = Instantiate(roundUFOTorpedo, transform.position, q);
newBullet.GetComponent<Rigidbody2D>().AddRelativeForce(new Vector2(0f, torpedoSpeed));
FindObjectOfType<AudioManager>().Play("RoundUFOTorpedo");
lastTimeShot = Time.time;
Destroy(newBullet, 5);
}
}
else
{
followingPlayer = false;
}
}
}