Skip to content

Instantly share code, notes, and snippets.

@toshikaz55
Created April 9, 2014 02:42
Show Gist options
  • Save toshikaz55/10221326 to your computer and use it in GitHub Desktop.
Save toshikaz55/10221326 to your computer and use it in GitHub Desktop.
Unityで2つ目のJoystick(Fire)を使って弾を発射するスクリプト
#pragma strict
var bulletPrefab : GameObject;
var initialVelocity : float = 40.0;
var canonY = 1.5;
var canonZ = 0.3;
private var direction : Vector3;
private var fireObject : GameObject;
private var fire : Joystick;
function Start () {
}
function Update () {
var doFire : boolean = false;
if ( Application.platform == RuntimePlatform.IPhonePlayer
|| Application.platform == RuntimePlatform.Android ) {
fireObject = GameObject.Find("Fire");
fire = fireObject.GetComponent(Joystick);
if( fire.IsFingerDown() ) {
doFire = true;
}
} else {
if( Input.GetButtonDown("Fire1") || Input.GetKey(KeyCode.Space) ) {
doFire = true;
}
}
if( doFire ) {
//Debug.Log("===== Fire1 =====");
var canonObj = GameObject.Find("canon");
//Debug.Log( "canonObj position = " + canonObj.transform.position );
//var Player : GameObject = GameObject.FindGameObjectWithTag( "Player" );
var position : Vector3 = canonObj.transform.position;
position.y = canonY;
//Debug.Log( "position = " + position );
//var bullet : GameObject = Instantiate( bulletPrefab, canonObj.transform.position, canonObj.transform.rotation );
//var bullet : GameObject = Instantiate( bulletPrefab, transform.position, transform.rotation );
var bullet : GameObject = Instantiate( bulletPrefab, position, canonObj.transform.rotation );
//Debug.Log( "bullet position = " + bullet.transform.position );
direction = transform.forward;
//var direction : Vector3 = ( canonObj.transform.position - transform.position ).normalized;
//var direction : Vector3 = Vector3( 0, 0, 1 );
//Debug.Log( "direction = " + direction );
bullet.rigidbody.velocity = direction * initialVelocity;
//bulletPrefab.rigidbody.velocity = Vector3( 0.0, 0.0, 1.0 ) * initialVelocity;
//bulletPrefab.transform.position += direction;
//Debug.Log( "velocity = " + bulletPrefab.rigidbody.velocity );
}
}
@toshikaz55
Copy link
Author

doFireはかっこ悪いな。
FireをFireBtnにして、doFireをFireにしたほうが良かった。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment