Skip to content

Instantly share code, notes, and snippets.

@atakotestudios
Last active October 26, 2017 00:58
Show Gist options
  • Save atakotestudios/c1cc23aed7a809a5b48de887544146a8 to your computer and use it in GitHub Desktop.
Save atakotestudios/c1cc23aed7a809a5b48de887544146a8 to your computer and use it in GitHub Desktop.
Cannon Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cannon : MonoBehaviour {
public GameObject projectile; //attach the projectile prefab here
public GameObject spawnpoint; //empty gameObject at position projectile should spawn
public float forceSpeed = 15f;
//SteamVR input
private SteamVR_TrackedObject trackedObj;
private SteamVR_Controller.Device device;
// Use this for initialization
void Start () {
if(GetComponentInParent<SteamVR_TrackedObject>() != null)
{
trackedObj = GetComponentInParent<SteamVR_TrackedObject>();
}
}
// Update is called once per frame
void Update ()
{
//if Trigger or Left Mouse pressed, Shoot
if (trackedObj != null)
{
device = SteamVR_Controller.Input((int)trackedObj.index);
if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
{
Shoot();
}
}
else if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
//Create Projectile at position of Cannon
GameObject liveProjectile =
Instantiate(projectile, spawnpoint.transform.position, transform.rotation);
//Shoot Projectile forward
liveProjectile.GetComponent<Rigidbody>().AddForce
(transform.forward * forceSpeed, ForceMode.Impulse); //Note: Impulse is affected by mass
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment