Created
June 22, 2015 03:25
-
-
Save Oxygamer/20100d06e11a8494042d to your computer and use it in GitHub Desktop.
Fast Bullet Script for Unity3d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class Bullet : MonoBehaviour | |
{ | |
public enum BulletType | |
{ | |
energy, | |
fire, | |
freeze, | |
laser, | |
missile, | |
flash, | |
} | |
private Vector3 _target; | |
private float Speed=2f; | |
private float distance = 0f; | |
private Vector3 startPosition; | |
public BulletType Type; | |
private float range=0f; | |
private int level; | |
private int Damage; | |
private int number; | |
private Transform T; | |
private Vector3 tempPosition=new Vector3(); | |
public int reflection_count = 0; | |
// Use this for initialization | |
void Awake() | |
{ | |
T = transform; | |
if (Random.value > 0.5f) | |
{ | |
number = 1; | |
} | |
else | |
{ | |
number = 0; | |
} | |
} | |
void Start () { | |
//_target=new Vector3(transform.position.x,transform.position.y,transform.position.z); | |
// iTween.MoveTo(gameObject,_target,1f); | |
Invoke("DestroyBullet",5f); | |
startPosition = T.position; | |
if (Type == BulletType.missile) | |
{ | |
Speed = 1.5f; | |
} | |
//SoundController.Instance.LaserSound(); | |
} | |
// Update is called once per frame | |
void FixedUpdate () { | |
//Fix performance | |
tempPosition.x =Mathf.Lerp(startPosition.x, _target.x,distance); | |
tempPosition.y = Mathf.Lerp(startPosition.y, _target.y, distance); | |
float mody=0f; | |
float modx = 0f; | |
if (Type == BulletType.energy) | |
{ | |
if (Mathf.Abs(_target.x) > Mathf.Abs(_target.y)) | |
{ | |
if (number == 0) | |
{ | |
mody = Mathf.Sin(tempPosition.x*0.5f) * 3; | |
} | |
else | |
{ | |
mody = Mathf.Cos(tempPosition.x*0.5f) * 3; | |
} | |
tempPosition.y += mody; | |
} | |
else | |
{ | |
if (number == 0) | |
{ | |
modx = Mathf.Sin(tempPosition.y*0.5f) * 5; | |
} | |
else | |
{ | |
modx = Mathf.Cos(tempPosition.y*0.5f) * 5; | |
} | |
tempPosition.x += modx; | |
} | |
} | |
if (Type == BulletType.flash) | |
{ | |
if (number == 0) | |
{ | |
mody = Mathf.Sin(tempPosition.x * 0.5f) * 20*distance; | |
} | |
else | |
{ | |
mody = Mathf.Cos(tempPosition.x * 0.5f) * 20*distance; | |
} | |
tempPosition.y += mody; | |
} | |
distance += Speed*0.01f; | |
T.position = tempPosition; | |
} | |
public void SetNumber(int c) | |
{ | |
number = c; | |
} | |
public void setTarget(Vector3 v) | |
{ | |
_target=v; | |
//Debug.Log("target is:"+_target); | |
} | |
public void setDamage(int damage) | |
{ | |
Damage = damage; | |
} | |
public int getDamage() | |
{ | |
return Damage; | |
} | |
public void SetLevel(int l) | |
{ | |
level = l; | |
} | |
public int GetLevel() | |
{ | |
return level; | |
} | |
public void setRange(float r) | |
{ | |
range = r; | |
} | |
void DestroyBullet() | |
{ | |
gameObject.SetActive(false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment