Projektile implementiert mit *KitchenGun*
This commit is contained in:
58
Assets/AssetsFORELLE/Script/KitchenGun.cs
Normal file
58
Assets/AssetsFORELLE/Script/KitchenGun.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
public class KitchenGun : MonoBehaviour
|
||||
{
|
||||
// The shot projectiles
|
||||
public GameObject projectilePrefab;
|
||||
// Time between shots, game needs to be restarted to take effect
|
||||
public float shotInterval = 1f;
|
||||
// Speed of projectiles
|
||||
public Vector2 shotSpeed = new Vector2(1f, 0f);
|
||||
// Smallest step size for shot speed
|
||||
private short shotSpeedStepSize = 50;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
InvokeRepeating("RecurringBang",0f,shotInterval);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void RecurringBang()
|
||||
{
|
||||
StartCoroutine("Bang");
|
||||
}
|
||||
|
||||
private IEnumerator Bang()
|
||||
{
|
||||
Transform t = transform;
|
||||
Vector3 position = t.position;
|
||||
|
||||
// create projectile
|
||||
GameObject projectile = Instantiate(projectilePrefab,
|
||||
position,
|
||||
t.rotation);
|
||||
// determine correct force vector
|
||||
Vector2 force = shotSpeed;
|
||||
if (t.localScale.x < 0) {
|
||||
force *= -1;
|
||||
}
|
||||
|
||||
force *= shotSpeedStepSize;
|
||||
|
||||
// add force to projectile
|
||||
Rigidbody2D pBody = projectile.GetComponent<Rigidbody2D>();
|
||||
pBody.AddRelativeForce(force);
|
||||
|
||||
yield return new WaitForSeconds(shotInterval);
|
||||
Debug.Log("Bang!");
|
||||
|
||||
Destroy(projectile);
|
||||
}
|
||||
}
|
11
Assets/AssetsFORELLE/Script/KitchenGun.cs.meta
generated
Normal file
11
Assets/AssetsFORELLE/Script/KitchenGun.cs.meta
generated
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4045b151b3272f3489865af8518b9e27
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user