Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ObjectGenerator : MonoBehaviour
  6. {
  7. Vector3 MousePosition, TargetPosition;
  8.  
  9. //To Instantiate TargetObject at mouse position
  10. public Transform TargetGameObject;
  11.  
  12. float distance = 10f;
  13.  
  14.  
  15.  
  16. // Update is called once per frame
  17. void Update()
  18. {
  19.  
  20. //To get the current mouse position
  21. MousePosition = Input.mousePosition;
  22.  
  23. //Convert the mousePosition according to World position
  24. TargetPosition = Camera.main.ScreenToWorldPoint(new Vector3(MousePosition.x, MousePosition.y, distance));
  25.  
  26. //Set the position of targetObject
  27. TargetGameObject.position = TargetPosition;
  28.  
  29. //Debug.Log(mousePosition+" "+targetPosition);
  30.  
  31.  
  32. //If Left Button is clicked
  33. if (Input.GetMouseButtonUp(0))
  34. {
  35. //create the instance of targetObject and place it at given position.
  36. Instantiate(TargetGameObject, TargetGameObject.transform.position, TargetGameObject.transform.rotation);
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement