Created
May 1, 2018 12:44
-
-
Save ditzel/23a2608201f2c38c0db42620facf91a5 to your computer and use it in GitHub Desktop.
Touch to Shoot
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; | |
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