Skip to content

Instantly share code, notes, and snippets.

@Redhucules
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Redhucules/f848e237849bafd0cc42 to your computer and use it in GitHub Desktop.
Save Redhucules/f848e237849bafd0cc42 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.Collections.Generic; //リストの追加
public class Bullet : MonoBehaviour {
private float AutoDestroyTime = 0;
private float AutoDestroyLimit =10;
public Object AutoDestroyEffect;
public Object HitEffect;
public float Ppower = 0;
public float Spower = 0;
public float Hpower = 0;
public float StabilityInfluence = 0;
public float BulletSpeed = 0;
public float BulletMOA = 0;
public float EffectiveDistance = 0;
public float OutRangePowerDecay = 0;
public List<float> BulletStats = new List<float>();
private float BulletMoveDistance = 0;
private Vector3 BulletCurrentPosition = Vector3.zero;
private Vector3 BulletPreviousPosition = Vector3.zero;
private Rigidbody MyRigidBody;
public GameObject BulletTrail;
void Awake()
{
MyRigidBody = GetComponent<Rigidbody>();
//弾丸性能入力
BulletStats.Add (Ppower);
BulletStats.Add (Spower);
BulletStats.Add (Hpower);
BulletStats.Add (StabilityInfluence);
}
// Use this for initialization
void Start ()
{
BulletCurrentPosition = transform.position;
BulletPreviousPosition = transform.position;
BulletShoot ();
}
// Update is called once per frame
void Update ()
{
BulletFlyingDistance ();
AutoDestory();
}
void AutoDestory()
{
//自然消滅射程限界
AutoDestroyTime += 1 * Time.deltaTime;
if (AutoDestroyTime >= AutoDestroyLimit)
{
if(AutoDestroyEffect != null)
{
Instantiate(AutoDestroyEffect,transform.position, Quaternion.identity);
}
Destroy (gameObject);
}
}
void BulletFlyingDistance()
{
//弾丸飛翔距離の計測
BulletCurrentPosition = transform.position;
BulletMoveDistance += Vector3.Distance (BulletCurrentPosition, BulletPreviousPosition);
BulletPreviousPosition = transform.position;
if (BulletMoveDistance >= EffectiveDistance) {
MyRigidBody.useGravity = true;
Ppower -= Ppower*(OutRangePowerDecay/10)*Time.deltaTime;
Spower -= Spower*(OutRangePowerDecay/10)*Time.deltaTime;
Hpower -= Hpower*(OutRangePowerDecay/10)*Time.deltaTime;
}
//攻撃力が無くなった時消滅
if (Ppower < 1 && Spower < 1 && Hpower < 1 )
{
if(AutoDestroyEffect != null)
{
Instantiate(AutoDestroyEffect,transform.position, Quaternion.identity);
}
if(BulletTrail != null)
{
BulletTrail.transform.parent = null;
}
Destroy (gameObject);
}
}
public void TriggerEnter(Collider HitCollider)
{
//DontGoThroughThingsの通知を使った接触判定
switch (HitCollider.gameObject.tag)
{
case "Player":
break;
case "Bullets":
break;
case "Terrain":
Instantiate(HitEffect,transform.position, Quaternion.identity);
if(BulletTrail != null)
{
BulletTrail.transform.parent = null;
}
Destroy (gameObject);
break;
case "Parts": //Parts_ArmorStatsに着弾判定を送る
BulletStatsUpdate();
HitCollider.gameObject.SendMessageUpwards("HitTest", BulletStats ,SendMessageOptions.DontRequireReceiver);
Instantiate(HitEffect,HitCollider.ClosestPointOnBounds(HitCollider.transform.position), Quaternion.identity);
if(BulletTrail != null)
{
BulletTrail.transform.parent = null;
}
Destroy (gameObject);
break;
default:
if(BulletTrail != null)
{
BulletTrail.transform.parent = null;
}
Destroy (gameObject);
break;
}
}
public void BulletShoot()
{
//弾道ブレ率と速度追加
transform.eulerAngles += new Vector3(Random.Range(-BulletMOA/100,BulletMOA/100),Random.Range(-BulletMOA/100,BulletMOA/100),0);
GetComponent<Rigidbody>().AddForce(transform.forward * BulletSpeed / 10f ,ForceMode.Impulse);
}
void BulletStatsUpdate(){
//弾丸性能リストの更新
BulletStats [0] = Ppower;
BulletStats [1] = Spower;
BulletStats [2] = Hpower;
BulletStats [3] = StabilityInfluence;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment