[英]Changing speed variable doesn't change the actual speed of the gameObject in Unity
我有一個附帶此腳本的簡單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;
}
}
}
我期望當我更改speed變量時gameObject會更改速度,而不會。 有什么問題?
在乘法之前標准化dir
分量:
dir = dir.normalized; // (or Vector3.Normalize(dir);)
transform.position += dir * Time.deltaTime * speed;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.