Created
October 18, 2016 19:37
-
-
Save sabotai/3e64adcc81accaed09fddbb39ac1c16f to your computer and use it in GitHub Desktop.
GD205 Fall16 Raycast Intro
This file contains hidden or 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 UnityEngine; | |
using System.Collections; | |
public class Scott : MonoBehaviour { | |
public float scottStrength = 1000f; | |
public AudioClip explosionSound; | |
public GameObject blueprint; //which prefab/gameobject would we like to create? | |
// Update is called once per frame | |
void Update () { | |
//cast a beam from our main camera to our mouseposition | |
//use the camera function ScreenPointToRay, using our "Input.mousePosition" inside the function | |
Ray beam = Camera.main.ScreenPointToRay (); | |
//draw a debug ray line so we can see it in unity editor | |
Debug.DrawRay(beam.origin, beam.direction * 1000f, Color.red); | |
//create a new raycasthit to store our hit information | |
RaycastHit beamHit = new RaycastHit (); | |
//if the ray "beam" is cast, did it hit something? if so, store the hit information in our beamHit | |
if (Physics.Raycast (beam, out beamHit, 1000f)) { | |
Debug.Log ("hit?"); //did it hit? just chexin' | |
if (Input.GetMouseButton (0) && beamHit.rigidbody) { //if the player left clicked and they clicked something with a rigidbody | |
Debug.Log ("has rigidbody"); | |
//add a force to the rigidbody in a random direction multiplied by the strength of 1000 scott summers | |
//insert "(Random.insideUnitSphere) * scottStrength" into the AddForce function below to make work | |
beamHit.rigidbody.AddForce (); | |
//also, insert our explosion audioclip into the audiosource of the thing hit | |
//at the end of the line, run the AudioSource function "PlayOneShot()" with our audioclip inside | |
beamHit.collider.gameObject.GetComponent<AudioSource> (); | |
} | |
if (Input.GetMouseButtonDown (1)) { //if the player has just pressed the right click | |
//instantiate ... blueprint ... at ... the location of the raycasthit ... with identity rotation | |
Instantiate (blueprint, beamHit.point, Quaternion.identity); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment