Created
September 15, 2017 19:19
-
-
Save danielgc/fb1880697f81ae251c9b1132d426539d to your computer and use it in GitHub Desktop.
FighterController.cs
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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class FighterController : MonoBehaviour { | |
public float speed = 10f; | |
public float padding = 1.3f; | |
public GameObject bullet; | |
// Use this for initialization | |
void Start () { | |
} | |
// Update is called once per frame | |
void Update () { | |
float hInput = Input.GetAxis ("Horizontal"); | |
transform.position += new Vector3 (hInput * speed * Time.deltaTime, 0, 0); | |
float vInput = Input.GetAxis ("Vertical"); | |
transform.position += new Vector3 (0, vInput * speed * Time.deltaTime, 0); | |
//clamping | |
float newX = Mathf.Clamp(transform.position.x, -10 + padding, 10 - padding); | |
float newY = Mathf.Clamp(transform.position.y, -10 + padding, 10 - padding); | |
transform.position = new Vector3 (newX, newY, transform.position.z); | |
// Detectar disparo | |
if (Input.GetKeyDown (KeyCode.Space)) { | |
// Ponemos en marcha el repetidor | |
InvokeRepeating("Fire", 0.001f, 0.15f); | |
} else if (Input.GetKeyUp (KeyCode.Space)) { | |
// Detenemos el repetidor | |
CancelInvoke("Fire"); | |
} | |
} | |
void Fire() { | |
var fighter = GameObject.Find ("Fighter"); | |
if (fighter != null) { | |
Vector3 newLeftPosition = fighter.transform.position + Vector3.up * .5f+ Vector3.left*.7f; | |
Vector3 newRightPosition = fighter.transform.position + Vector3.up * .5f+ Vector3.right*.7f; | |
Instantiate (bullet, newLeftPosition, Quaternion.identity); | |
Instantiate (bullet, newRightPosition, Quaternion.identity); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment