I wrote a script which checks to see if a moving object would get hit by a projectile launched from a stationary object based on the velocities, initial positions, and angles of the given objects when the mouse is clicked rather then using 2D colliders and checking for collisions. I was hoping that by doing this the processing would be decreased since it isn’t checking frame by frame rather only when the mouse is clicked. I am wonder whether or not this is actually the case. I am planning on making a game which will have many moving objects (anywhere from 1 to 30+) and want it to be able to be run on mobile devices and am not sure how much having many colliders effects processing vs a mathmatical computation.
Code I wrote:
using UnityEngine;
using System.Collections;
public class Meteor : MonoBehaviour {
public float halfSpriteLength;
private float Ax;
private float By;
public float speed = 8;
private float x;
public Rotation rotation;
public float t;
private float tx;
private float ty;
private float X;
private float X1;
private float X2;
private float Xi1;
private float Xi2;
private float Xe1;
private float Xe2;
private Rigidbody2D rigid;
private float Speed;
public float R;
// Use this for initialization
void Start ()
{
rigid = GetComponent<Rigidbody2D> ();
rigid.velocity = transform.right * speed;
Speed = GameObject.Find("Barrel").GetComponent<Rotation>().speed;
rotation = GameObject.Find ("Barrel").GetComponent<Rotation> ();
}
// Update is called once per frame
void Update ()
{
R = rotation.rotZ;
x = rigid.transform.eulerAngles.z;
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Check ();
if (transform.position.x < 0)
{
if (x + 180 < R)
{
if ((R < Xi2 && R > Xe2))
{
Destroy (gameObject, t);
}
}
if (x + 180 > R)
{
if ((rotation.rotZ > Xi2 && rotation.rotZ < Xe2))
{
Destroy (gameObject, t);
}
}
}
if (transform.position.x > 0) {
if (x + 180 > R) {
if ((R < Xi1 && R > Xe1)) {
Destroy (gameObject, t);
}
}
if (x + 180 < R) {
if ((rotation.rotZ > Xi1 && rotation.rotZ < Xe1)) {
Destroy (gameObject, t);
}
}
}
}
}
void Check ()
{
float Ai = transform.position.x;
float Bi = transform.position.y;
float Ci = (-Bi * rigid.velocity.x + Ai * rigid.velocity.y)/Speed;
tx = Mathf.Abs((transform.position.x) / (Speed*Mathf.Cos(R*Mathf.Deg2Rad) - rigid.velocity.x));
ty = Mathf.Abs((transform.position.y) / (Speed*Mathf.Sin(R*Mathf.Deg2Rad) - rigid.velocity.y));
if (ty > tx)
{
t = ty;
}
else
{
t = tx;
}
X1 = Mathf.Rad2Deg*(Mathf.Acos ((-(Bi * Ci) + Mathf.Sqrt ((Mathf.Pow (Ai, 4)) + (Mathf.Pow (Ai, 2) * Mathf.Pow (Bi, 2)) - (Mathf.Pow (Ai, 2) * Mathf.Pow (Ci, 2)))) / (Mathf.Pow (Ai, 2) + Mathf.Pow (Bi, 2))));
X2 = Mathf.Rad2Deg*(Mathf.Acos ((-(Bi * Ci) - Mathf.Sqrt ((Mathf.Pow (Ai, 4)) + (Mathf.Pow (Ai, 2) * Mathf.Pow (Bi, 2)) - (Mathf.Pow (Ai, 2) * Mathf.Pow (Ci, 2)))) / (Mathf.Pow (Ai, 2) + Mathf.Pow (Bi, 2))));
Xi1 = X1 + Mathf.Rad2Deg*(Mathf.Atan2 (halfSpriteLength, (Speed * t)));
Xi2 = X2 - Mathf.Rad2Deg*(Mathf.Atan2 (halfSpriteLength, (Speed * t)));
Xe1 = X1 - Mathf.Rad2Deg*(Mathf.Atan2 (halfSpriteLength, (Speed * t)));
Xe2 = X2 + Mathf.Rad2Deg*(Mathf.Atan2 (halfSpriteLength, (Speed * t)));
}
Input would be apreciated.