MouseInstantiate.cs 868 B

123456789101112131415161718192021222324252627282930
  1. // Shatter Toolkit
  2. // Copyright 2015 Gustav Olsson
  3. using UnityEngine;
  4. namespace ShatterToolkit.Helpers
  5. {
  6. public class MouseInstantiate : MonoBehaviour
  7. {
  8. public GameObject prefabToInstantiate;
  9. public float speed = 7.0f;
  10. public void Update()
  11. {
  12. if (Input.GetMouseButtonDown(0) && prefabToInstantiate != null)
  13. {
  14. Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
  15. GameObject newGameObject = (GameObject)Instantiate(prefabToInstantiate, mouseRay.origin, Quaternion.identity);
  16. Rigidbody rb = newGameObject.GetComponent<Rigidbody>();
  17. if (rb != null)
  18. {
  19. rb.velocity = mouseRay.direction * speed;
  20. }
  21. }
  22. }
  23. }
  24. }