Skip to content

Instantly share code, notes, and snippets.

@sabotai
Created November 1, 2016 20:56
Show Gist options
  • Save sabotai/70b43f9204ef8a8688829256d0ec1ca4 to your computer and use it in GitHub Desktop.
Save sabotai/70b43f9204ef8a8688829256d0ec1ca4 to your computer and use it in GitHub Desktop.
Cannon Action from GD205 Week 9
using UnityEngine;
using System.Collections;
public class CannonBoom : MonoBehaviour {
public GameObject projBlueprint;
public Transform projOrigin;
Quaternion newRotation;
public GameObject cannon;
public float projForce = 300f;
bool doRotate;
float rotPct;
public int whichButton;
void Start () {
doRotate = false;
rotPct = 0f;
}
void Update () {
Ray ourRay = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit rayHit = new RaycastHit ();
Debug.DrawRay(ourRay.origin, ourRay.direction * 1000f, Color.magenta);
if (Physics.Raycast (ourRay, out rayHit, 1000f) && Input.GetMouseButtonDown (whichButton)) {
Debug.Log ("you dun hit smthnggg");
newRotation = Quaternion.LookRotation (rayHit.point - cannon.transform.position);
doRotate = true;
rotPct = 0f;
}
if (doRotate) {
float rotSpeed = Time.deltaTime;
rotPct += rotSpeed;
Debug.Log ("Barrel rotated by ... " + rotPct * 100f + "%");
if (rotPct < 1f) {
cannon.transform.rotation = Quaternion.Slerp (cannon.transform.rotation, newRotation, rotSpeed);
} else {
GameObject newProj = Instantiate (projBlueprint, projOrigin.transform.position, projOrigin.rotation) as GameObject;
newProj.GetComponent<Rigidbody> ().AddForce (projOrigin.forward * projForce);
doRotate = false;
cannon.GetComponent<AudioSource> ().Play ();
StartCoroutine(ScreenShake.Shake(0.25f, 0.5f));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment