Does anyone know where I can find resources for how to calculate a curve? I’m trying to instantiate an object then have it choose a random angle, random 360 degree direction, and random speed to send it off in a direction. Similar to if you were firing an object out of a cannon but without the cannon being there. I want to use x,y,z positions instead of physics or rigidbody force as well.
var bullet : GameObject;
function Start()
{
var x = Random.Range(180,-180);
var y = same as that ^^^;
var z = same as this ^^^;
Instantiate(bullet, transform.position, Vector3(x,y,z));
}
You should use a polynomial equition to do this. The code below I used to draw a curve with small plane particles. You can replace the part in the update function with your own equation, which will probably be a parabola, rather than the one I used. Also, yours might not be in the update function, except if you want to change the angle or curve in realtime. But, please, this is only if YOU want to determine the path yourself.
Here you go, hope this helps!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class lazer_beam : MonoBehaviour {
public List<Vector3> points = new List<Vector3>();
public List<GameObject> planes = new List<GameObject>();
float const_a;
float const_b;
float const_c;
void Start ()
{
LoadPlanes();
SetupPoints();
StartCoroutine("ChangeLazer");
}
void LoadPlanes()
{
for (int i = 0; i < 100; i++)
{
GameObject go = (GameObject)Instantiate(Resources.Load("prefabs/lazer_particle"));
planes.Add(go);
go.transform.localEulerAngles = transform.localEulerAngles;
go.transform.localScale = new Vector3(0.008f, 1, 0.008f);
go.layer = 9;
//Destroy(go.GetComponent<MeshCollider>());
go.transform.parent = transform;
}
}
void SetupPoints()
{
for (int i = 0; i < 100; i++)
{
points.Add(new Vector3(transform.position.x + (i * 0.02f), transform.position.y, transform.position.z));
planes[i].transform.position = points[i];
}
}
void Update()
{
float x = 0;
float y = 0;
for (int i = 0; i < 100; i++)
{
x = transform.position.x + (i * 0.01f) - 0.5f;
y = (x * x * x * x * const_a) - (x * x * x * const_b) + 0.5f;
Vector3 temp = new Vector3(x, y, transform.position.z);
planes[i].transform.position = temp;
}
}
IEnumerator ChangeLazer()
{
yield return new WaitForSeconds(1);
const_a = Random.Range(-10, 10);
const_b = Random.Range(-10, 10);
StartCoroutine("ChangeLazer");
}
}
Thank you! This was the kind of code I was hoping someone would post. I’m trying to make patterns like bullet hell games so would this be the start of that kinda?
That’s what most people call them. They are the 2d top down games where you control a plane usually and avoid large amounts of bullets. Everytime the bullets are fired they usually follow a certain pattern.
The example you gave, seems to me like those bullets are actually moving straing and you get the illusion that they are moving at a curve when they are spawned fast in succsession. So all they are doing there is to change the x position ( and y obviously) of the bullet, based on the x_speed of the player plain. Thinking about my script again, creating an ground-to-ground trajectory using a parabola might give you problems with interpolating smoothly between the points on the curve. And that is a dark art I must still do at some stage for one of my projects:)