C# 将游戏对象缓慢移动到鼠标位置

C# 将游戏对象缓慢移动到鼠标位置,c#,unity3d,C#,Unity3d,我想将对象的位置更改为鼠标的位置,从第一个位置缓慢移动到第二个位置 我的物体慢慢地向随机方向移动,似乎与左下角相连。当我走到高于角的位置时,我的物体向上移动,左右移动也是一样 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Rocket : MonoBehaviour { public float speed = 10f; private V

我想将对象的位置更改为鼠标的位置,从第一个位置缓慢移动到第二个位置

我的物体慢慢地向随机方向移动,似乎与左下角相连。当我走到高于角的位置时,我的物体向上移动,左右移动也是一样

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;
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共级火箭:单一行为
{
公共浮子速度=10f;
私人向量机3 shippos;
void Start()
{
shippos=变换位置;
}
void FixedUpdate()
{
if(Input.mousePosition.x>shippos.x)
shippos.x=shippos.x+速度*时间增量;
if(Input.mousePosition.xshippos.y)
shippos.y=shippos.y+速度*时间增量时间;
if(Input.mousePosition.y
如果我没有误解的话,您希望将玩家的位置直接更改到鼠标的位置并朝鼠标看

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.
}

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

void FixedUpdate()
{
if(Camera.main.ScreenToWorldPoint(Input.mousePosition.x>shippos.x)
shippos.x=shippos.x+速度*时间增量;
if(Camera.main.ScreenToWorldPoint(Input.mousePosition).xshippos.y)
shippos.y=shippos.y+速度*时间增量时间;
if(Camera.main.ScreenToWorldPoint(Input.mousePosition).y
取自此处:


可能不准确,但你明白了。

我想你做错了,因为你有你不需要走那种笨拙的路
这是youtube上的视频教程


下面是基本代码:

someValue = Mathf.Lerp(initialValue , finalValue , Time.deltaTime * smoothness);
只要看看youtube的链接,你一定会明白的!

作为旁注

你不需要做很多事情来降低你的游戏性能对这种编码要小心

你似乎在问3个不同的问题。同时,也不完全清楚你在这三者中想要实现什么。你可以编辑你的问题,以便清楚你需要帮助完成什么。@Ryemos编辑,也许现在更清楚了?问题或OP的代码中没有提到任何关于旋转的内容(并且你的代码没有移动任何游戏对象)。我想慢慢移动它,而不是通过传送到其他位置(我想连续移动鼠标的方向)成功了!感谢您对屏幕空间到世界坐标转换的解释。Lerp将平滑地移动对象,但它会改变它的移动速度。(对于小的移动速度将很小,对于大的移动速度,我们将看到巨大的变化)MoveToward对我来说更好。但我的主要问题是关于ScreenToWorldPoint(Input.mousePosition.x)。这就是我被卡住的地方。
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);
     }
someValue = Mathf.Lerp(initialValue , finalValue , Time.deltaTime * smoothness);