更改速度变量不会改变Unity

时间:2016-09-07 09:43:51

标签: c# unity3d

我有一个附带此脚本的简单gameObject。它会循环遍历每个位置,使用 currentPos 变量定义。

using UnityEngine;
using System.Collections;

public class PathFollower : MonoBehaviour {
    public Transform[] path;
    public float speed = 4.0f;
    public float breakArea = 1.0f;
    public int currentPos = 0;

    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {
        Debug.Log(speed);
        Vector3 dir = path[currentPos].position - transform.position;
        transform.position += dir * Time.deltaTime * speed;

        if (dir.magnitude <= breakArea)
        {
            currentPos++;
        }
        if (currentPos >= path.Length)
        {
            currentPos = 0;
        }
    }
}

我期待gameObject在我改变速度变量时改变速度,而不是。有什么问题?

1 个答案:

答案 0 :(得分:0)

在乘以之前规范化dir分量:

dir = dir.normalized; // (or Vector3.Normalize(dir);)
transform.position += dir * Time.deltaTime * speed;