I am currently working on a 2D platformer video game in Unity for a project at school, and I am making a boss that throws lobbed objects at the player. I was wanting to make the speed of the objects change randomly to add a little challenge, that way the projectiles are hitting in different spots. This should allow the player to have to do more dodging than if it was hitting the same spot. The boss is going to be stationary, as well. The only problem that I am facing is making the speed vary randomly. I do not know anything about C# and have to get this done in two days of posting this. How would I change my code to allow me to make a random change to the speed?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyProjectile : MonoBehaviour
{
public Vector2 bossMoveSpeed = new Vector2(3f, 0); //adjusts the speed. This is where I am having the problem.
public int bossDamage;
Rigidbody2D bossRB;
private void Awake()
{
bossRB = GetComponent<Rigidbody2D>();
}
private void Start()
{
bossRB.velocity = new Vector2(bossMoveSpeed.x * transform.localScale.x, bossMoveSpeed.y); // applies the speed to the RigidBody2D of the projectile. If I try to make it an update instead of Start for the Randomness per shot, it just goes straight, and it needs to be lobbed.
}
private void OnTriggerEnter2D(Collider2D collision) //for damage and for the destruction of the object.
{
PlayerHealth health = collision.GetComponent<PlayerHealth>();
if (collision.gameObject.CompareTag("Player"))
{
health.TakeDamage(bossDamage);
}
Destroy(gameObject);
}
}