简体   繁体   English

团结:游戏的特殊运动

[英]Unity: Special movement for game

I'm writing movement for my space game and spaceship object (player) with mouse cursor. 我正在用鼠标光标为我的太空游戏和飞船对象(玩家)编写运动。

Currently have following code: 目前有以下代码:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class Move : MonoBehaviour {

    public float speed = 1.5f;
    public float rotationSpeed = 90f;
    public float rotPrecision = 0.1f;
    public float movePrecision = 0.1f;
    private Vector3 pos;
    private Quaternion qTo;

    void Start () {
        pos = transform.position;
        qTo = transform.rotation;
    }

    void Update () {
        if (!EventSystem.current.IsPointerOverGameObject())
        {

            if (Input.GetMouseButtonDown(0) || Input.GetMouseButton(0))
            {
                pos = Input.mousePosition;
                pos.z = transform.position.z - Camera.main.transform.position.z;
                pos = Camera.main.ScreenToWorldPoint(pos);
            }

            var dir = pos - transform.position;
            qTo = Quaternion.LookRotation(Vector3.forward, pos - transform.position);

            if (Quaternion.Angle(transform.rotation, qTo) >= rotPrecision) //just set your own precision
                transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, Time.deltaTime * rotationSpeed);

            if (Vector3.Distance(transform.position, pos) > movePrecision) // 0.1f
                transform.Translate(Vector3.up * speed * Time.deltaTime);
        }
    }    

}

But there i have the problem with the movement precision and rotation when the point is too close to player (have infinite loop). 但是当点离玩家太近(有无限循环)时,我的运动精度和旋转存在问题。

The idea of this movement system described with the following image: 下图描述了此运动系统的概念:

(Player actor is green, path is gray, and destination point is red). (玩家角色为绿色,路径为灰色,目标点为红色)。

行动计划

I hope that somebody could help me w/ that. 我希望有人可以帮助我。

Thank you! 谢谢!

If I understand your question correctly, the problem is that the player's movement never stops as the code can't reach a finishing point. 如果我正确地理解了您的问题,那么问题就在于玩家的动作永远不会停止,因为代码无法达到终点。

To solve this you can add an acceptable precision margin. 为了解决这个问题,您可以添加一个可接受的精度裕度。

So calculate if the difference between the rotation you wish or the movement you wish, and the players actual rotation/position, is less than a given variable, for example less than 0.05%. 因此,请计算您希望的旋转或希望的运动与玩家实际的旋转/位置之间的差是否小于给定变量,例如小于0.05%。

That way you could allow the program to know that if it's just within 0.05% precision, then it's okay for it to stop moving. 这样,您可以让程序知道,如果它的精确度仅在0.05%以内,则可以停止移动。

Otherwise, if the program never reaches a complete and perfect rotation and position, it will continue to adjust endlessly due to slight mathematical imprecision in the calculations and movement pattern. 否则,如果程序从未达到完整和完美的旋转和位置,则由于计算和运动方式略有数学上的不精确性,它将继续不断地进行调整。

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

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