在鼠标位置实例化对象 [英] Instantiate Object at mouse position

查看:108
本文介绍了在鼠标位置实例化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个脚本,该脚本应该根据鼠标位置实例化游戏对象,但是出了点问题.而且它仅在屏幕中间的一个位置被实例化.

I have created a script which was supposed to instantiate gameobject according to mouse position but something has went wrong. And it is only being instantiated at one position and in the middle of the screen.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LineInstantiater : MonoBehaviour {

    public GameObject lineprefab;
    private GameObject linehandler;
    private Vector3 mousepos;

    void Update(){
        if (Input.GetMouseButton (0)) {
            mousepos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
            linehandler = Instantiate (lineprefab,Camera.main.ScreenToWorldPoint(Input.mousePosition),Quaternion.identity) as GameObject ;
            linehandler.transform.position = mousepos;
        }
    }

}

请告诉我我的脚本出了什么问题.

Please tell me what is wrong with my script.

推荐答案

问题是Input.mousePosition没有z轴,因为鼠标坐标只有x和y轴. z轴就是0,因此当使用Camera.main.ScreenToWorldPoint时返回错误的位置.

The problem is that Input.mousePosition does not have z-axis because there is only x and y axis for mouse coordinate. The z axis is simply 0 therefore returning wrong position when Camera.main.ScreenToWorldPoint is used.

您需要执行Input.mousePosition;,将其z轴值手动修改为任何> 0.通常,10可以解决此问题,但是如果您不足以对其进行修改,则可以对其进行修改.之后,您可以将修改后的Vector3传递给Camera.main.ScreenToWorldPoint(mousepos)函数.

You need to do Input.mousePosition;, manually modify it's z-axis value to be anything > 0. Usually, 10 is fine for this but you can modify it if it is not enough for you. After that, you can then pass that modified Vector3 to the Camera.main.ScreenToWorldPoint(mousepos) function.

public GameObject lineprefab;
private GameObject linehandler;
private Vector3 mousepos;

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        mousepos = Input.mousePosition;
        mousepos.z = 10;

        mousepos = Camera.main.ScreenToWorldPoint(mousepos);
        linehandler = Instantiate(lineprefab, mousepos, Quaternion.identity) as GameObject;
    }
}

OR

public GameObject lineprefab;
private GameObject linehandler;

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        Ray rayCast = Camera.main.ScreenPointToRay(Input.mousePosition);
        linehandler = Instantiate(lineprefab, rayCast.GetPoint(10), Quaternion.identity) as GameObject;
    }
}

不相关:

我注意到您正在使用Input.GetMouseButton.您可能希望Input.GetMouseButtonDown,因为Input.GetMouseButtonDown被调用一次,直到释放键.按住Input.GetMouseButton键时反复调用Input.GetMouseButton,您可以轻松地用它创建数千个对象.

I noticed that you are using Input.GetMouseButton. You probably want Input.GetMouseButtonDown as Input.GetMouseButtonDown is called once until the key is released. Input.GetMouseButton is repeatedly called when the pressed key is held down and you can easily create thousands of objects with that.

这篇关于在鼠标位置实例化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆