Skip to content

Instantly share code, notes, and snippets.

@cadenburleson
Created February 19, 2017 10:39
Show Gist options
  • Save cadenburleson/35a3d4a5b577155eff057b68bc984b99 to your computer and use it in GitHub Desktop.
Save cadenburleson/35a3d4a5b577155eff057b68bc984b99 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunFollow: MonoBehaviour {
public GameObject objectToSpawn;
public Transform gun;
public Transform cam;
// Update is called once per frame
void Update () {
Ray ray = new Ray(cam.position, cam.forward);
RaycastHit hit;
if( Physics.Raycast( ray, out hit, Mathf.Infinity )) {
gun.LookAt (hit.point);
if (Input.GetButton("Fire1")) {
Instantiate( objectToSpawn, hit.point, Quaternion.LookRotation( hit.normal ) );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment