Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class ObjectGenerator : MonoBehaviour
- {
- Vector3 MousePosition, TargetPosition;
- //To Instantiate TargetObject at mouse position
- public Transform TargetGameObject;
- float distance = 10f;
- // Update is called once per frame
- void Update()
- {
- //To get the current mouse position
- MousePosition = Input.mousePosition;
- //Convert the mousePosition according to World position
- TargetPosition = Camera.main.ScreenToWorldPoint(new Vector3(MousePosition.x, MousePosition.y, distance));
- //Set the position of targetObject
- TargetGameObject.position = TargetPosition;
- //Debug.Log(mousePosition+" "+targetPosition);
- //If Left Button is clicked
- if (Input.GetMouseButtonUp(0))
- {
- //create the instance of targetObject and place it at given position.
- Instantiate(TargetGameObject, TargetGameObject.transform.position, TargetGameObject.transform.rotation);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement