Skip to content

Instantly share code, notes, and snippets.

@ditzel
Created May 1, 2018 12:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ditzel/23a2608201f2c38c0db42620facf91a5 to your computer and use it in GitHub Desktop.
Save ditzel/23a2608201f2c38c0db42620facf91a5 to your computer and use it in GitHub Desktop.
Touch to Shoot
using UnityEngine;
public class TouchToShoot : MonoBehaviour {
public Material hitMaterial;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
var rig = hitInfo.collider.GetComponent<Rigidbody>();
if(rig != null)
{
rig.GetComponent<MeshRenderer>().material = hitMaterial;
rig.AddForceAtPosition(ray.direction * 50f, hitInfo.point, ForceMode.VelocityChange);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment