【问题标题】:Slowly moving gameobject to mouse position慢慢将游戏对象移动到鼠标位置
【发布时间】:2017-09-06 15:12:45
【问题描述】:

我想把物体的位置换成鼠标的位置,从第一个位置慢慢移动到第二个位置。

我的对象正在缓慢地向似乎与左下角相连的随机方向移动。当我高于拐角时,我的对象向上移动,左右相同。

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

public class Rocket : MonoBehaviour
{
    public float speed = 10f;

    private Vector3 shippos;

    void Start()
    {
        shippos = transform.position;
    }

    void FixedUpdate()
    {
        if (Input.mousePosition.x > shippos.x)
            shippos.x=shippos.x+speed*Time.deltaTime;
        if (Input.mousePosition.x < shippos.x)
            shippos.x=shippos.x-speed*Time.deltaTime;
        if (Input.mousePosition.y > shippos.y)
            shippos.y=shippos.y+speed*Time.deltaTime;
        if (Input.mousePosition.y < shippos.y)
            shippos.y=shippos.y-speed*Time.deltaTime;
        transform.position = shippos;
    }
}

【问题讨论】:

  • 您似乎在问 3 个不同的问题。也不完全清楚你在这三者中想要达到的目标。您能否编辑您的问题,以便清楚您需要帮助完成什么。
  • @ryemoss 已编辑,也许现在更清楚了?

标签: c# unity3d


【解决方案1】:

鼠标位置以屏幕空间坐标返回。您需要做的是将此转换为世界坐标,以便它们在与变换 (shippos) 相同的坐标空间中进行比较。

void FixedUpdate()
{
    if (Camera.main.ScreenToWorldPoint(Input.mousePosition).x > shippos.x)
        shippos.x = shippos.x + speed * Time.deltaTime;
    if (Camera.main.ScreenToWorldPoint(Input.mousePosition).x < shippos.x)
        shippos.x = shippos.x - speed * Time.deltaTime;
    if (Camera.main.ScreenToWorldPoint(Input.mousePosition).y > shippos.y)
        shippos.y = shippos.y + speed * Time.deltaTime;
    if (Camera.main.ScreenToWorldPoint(Input.mousePosition).y < shippos.y)
        shippos.y = shippos.y - speed * Time.deltaTime;
    transform.position = shippos;
}

【讨论】:

  • 成功了!感谢您对屏幕空间到世界坐标转换的解释。
【解决方案2】:

如果我没听错的话,你是想把你的玩家的位置直接改变到鼠标的位置并看向鼠标。

void Update() {
    Transform target = mousePosition; //get your mouse position per frame
    Vector3 relativePos = target.position - transform.position; //create a vector3 between them
    Quaternion rotation = Quaternion.LookRotation(relativePos); //then give a rotation your player towards this vector.
    transform.rotation = rotation; //and apply it.
}

【讨论】:

  • 问题或 OP 的代码中没有提到任何关于旋转的内容(并且您的代码不会移动任何游戏对象)
  • 我想慢慢移动它,而不是通过传送到其他位置(我想连续移动到鼠标的方向)
【解决方案3】:

取自这里:http://answers.unity3d.com/questions/633873/how-to-make-an-object-move-towards-the-mouse-point.html

public float moveSpeed = 2.0;  // Units per second

 void Update () {
         var targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         targetPos.z = transform.position.z;
         transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
     }

可能不是准确的 C#,但你明白了。

【讨论】:

    【解决方案4】:

    我猜你做错了,因为如果你有 Mathf.Lerp,你就不需要走那种笨拙的方式
    这是来自 youtube 的 mathf.lerp 的视频教程


    这是基本代码:

    someValue = Mathf.Lerp(initialValue , finalValue , Time.deltaTime * smoothness);
    

    只要看看 youtube 链接,你一定会明白的!

    作为旁注
    你不需要做很多事情来降低你的游戏性能!小心这种编码!

    【讨论】:

    • Lerp 会平滑移动物体,但会改变它的移动速度。 (对于小移动速度会很小,对于更大的移动,我们会看到巨大的变化)MoveTowards 对我来说更好。但我的主要问题是关于 ScreenToWorldPoint(Input.mousePosition.x)。这就是我卡住的地方。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 2012-06-02
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多