简体   繁体   English

用鼠标移动对象但不移动到鼠标指针所在的位置

[英]moving object with mouse but not to the where mouse pointer is

Scene:场景:场景图像

Hi I'm trying to control the hole along the X and Z axis with using the mouse but don't want the hole moving to the mouse position嗨,我正在尝试使用鼠标控制沿 X 轴和 Z 轴的孔,但不希望孔移动到鼠标位置

Plane m_Plane;平面 m_Plane;

private Vector3 newmousepos;


void Start()

{

    m_Plane = new Plane(Vector3.up, transform.position);

}

void Update()

{
    if (Input.GetMouseButton(0))

    {

        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

          float enter = 0.0f;

          if (m_Plane.Raycast(ray, out enter))

           {
               //Get the point that is clicked
               Vector3 hitPoint = ray.GetPoint(enter);

              transform.position = 
              Vector3.Lerp(transform.position,hitPoint,Time.deltaTime*5f);

           }  

    }

Edit: New answer based on new informaton编辑:基于新信息的新答案

I will keep the old answer since the code is not functional without it, if "move towards mouse position" would be the desired behaviour, as the title suggests.我将保留旧答案,因为如果没有它,代码将无法运行,如果“向鼠标位置移动”将是所需的行为,如标题所示。

Problem问题

You're trying to get the object to do the same movements as the mouse is doing, but the code you've written takes in the position of the mouse, not the movement of the mouse, and moves towards it.您试图让对象做与鼠标相同的运动,但是您编写的代码接受了鼠标的位置,而不是鼠标的移动,并向它移动。

Solution解决方案

You must check the axis of the mouse, and apply that movement direction to the object, something like:您必须检查鼠标的轴,并将该移动方向应用于对象,例如:

float speed = 5f;
void Update()
{
    if (Input.GetMouseButton(0))
    {
        var moveDirection = new Vector3(Input.GetAxis("Mouse X"), 0, Input.GetAxis("Mouse Y"));
        transform.position += moveDirection * speed * Time.deltaTime;
    }
}


Old answer旧答案

Problem问题

I'm guessing your Lerp isn't doing what you want it to do, since the code will only execute once, and is setting your position to Time.deltaTime*5, which should roughly be 0.9, which means 90% of the distance from transform.position to hitPoint .我猜你的 Lerp 没有做你想做的事,因为代码只会执行一次,并将你的位置设置为 Time.deltaTime*5,它应该大约是 0.9,这意味着距离的 90%从transform.positionhitPoint For more information about Lerping, please read the documentation :有关 Lerping 的更多信息,请阅读文档

Vector3 Lerp(Vector3 a, Vector3 b, float t); Vector3 Lerp(Vector3 a, Vector3 b, float t);

  • When t = 0: returns a.当 t = 0 时:返回 a。
  • When t = 1 returns b.当 t = 1 时返回 b。
  • When t = 0.5 returns the point midway between a and b.当 t = 0.5 时返回 a 和 b 之间的中间点。

Solution解决方案

I think a simple Vector3.MoveTowards will work for you:我认为一个简单的Vector3.MoveTowards对你有用

Vector3 hitPoint;
float speed = 5;

void Update()
{
    if (Input.GetMouseButton(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        float enter = 0.0f;

        if (m_Plane.Raycast(ray, out enter))
        {
            hitPoint = ray.GetPoint(enter);
            transform.position = Vector3.MoveTowards(transform.position, hitPoint, speed * Time.deltaTime);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM