- Home /
Question by
YankoxD · Jun 08, 2015 at 08:42 AM ·
spawning problems
How to spawn cubes on mouse position
Hello i use this code to spawn cubes but they spawn next to the cube i attached the rigidbody so i want they to spawn on mouse position :
using UnityEngine;
using System.Collections;
public class IfClose : MonoBehaviour {
public bool Gravitronned;
public Rigidbody projectile;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
RaycastHit hit;
if (Input.GetKeyDown (KeyCode.F)){
Rigidbody clone;
clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
clone.velocity = transform.TransformDirection(Vector3.forward * 10);
}
}
}
Comment
Answer by revolute · Jun 08, 2015 at 09:15 AM
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
You may want to change z value for pos too.
Now that you have screen position, Instantiate your prefab at that position.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200710im_/https://answers.unity.com/themes/thub/images/avi.jpg)