// 27 Slicer // Copyright 2021 Deftly Games // https://slicer.deftly.games/ using UnityEngine; using UnityEngine.EventSystems; namespace Slicer.Demo { /// /// A simple script that can be used to throw a prefab, normally a ball. /// [AddComponentMenu("Slicer/Demo/Prefab Thrower")] public class PrefabThrower : MonoBehaviour { /// /// The prefab to throw /// public GameObject PrefabToThrow; private Camera mainCamera; /// /// The speed to throw the prefab at /// public float Speed = 10f; private void Awake() { mainCamera = Camera.main; } private void Update() { if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject()) { Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); var pos = ray.origin + (ray.direction * 0.25f); var objectThrown = Instantiate(PrefabToThrow, pos, Quaternion.identity, transform); var rigidBody = objectThrown.GetComponent(); rigidBody.linearVelocity = ray.direction * (Speed); Destroy(objectThrown, 10f); } } } }