这是我的代码:
public class CharacterController : MonoBehaviour
{
private Vector3 _startLocation = Vector3.zero;
private Vector3 _currentLocation = Vector3.zero;
private Vector3 _endLocation = Vector3.zero;
private bool _isMoving = false;
private float _distanceToTravel;
private float _startTime;
public float Speed = 1.0f;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Left mouse button clicked");
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.CompareTag("Ground"))
{
_startLocation = transform.position;
_endLocation = hit.point;
_isMoving = true;
_startTime = Time.time;
_distanceToTravel = Vector3.Distance(_startLocation, _endLocation);
Debug.Log(string.Format("Ground has been hit: Start: {0}, End: {1}", _startLocation.ToString(), _endLocation.ToString()));
}
}
}
if (_isMoving)
Move();
}
void Move()
{
float timeElapsed = (Time.time - _startTime) * Speed;
float t = timeElapsed / _distanceToTravel;
_currentLocation = Vector3.Lerp(_startLocation, _endLocation, t);
transform.Translate(_currentLocation);
if (_currentLocation == _endLocation)
{
Debug.Log(string.Format("Destination reached ({0})", _endLocation.ToString()));
_isMoving = false;
}
}
}
我阅读了Vector3.Lerp
函数以及Physics.Raycast
函数的文档,最后得到了此代码。
调试控制台确认地面已被击中,但我的胶囊开始向Y方向上移动并且永不停止!
我对Unity和游戏开发一般都很陌生,所以我还在学习,但是关于我做错了什么的指示?
答案 0 :(得分:1)
因为你使用了transform.Translate。它正在Y中移动。
transform.Translate正在移动它,所以如果你做了transform.Translate(0,0,10) 它会在z中移动,如果你确实转换了.Translate(0,10,10) 它会在y和z方向上移动。
所以为了解决这个问题,我将向您展示两种方式:
1)使用Vector3.Lerp:
transform.position = Vector3.Lerp(_startLocation, _endLocation, t);
2)使用MoveTowards:
transform.position = Vector3.MoveTowards(transform.position, _endLocation, Speed * Time.deltaTime);
在第一个Vector3.Lerp中使用,我也看到你也使用它了
_currentLocation = Vector3.Lerp(_startLocation, _endLocation, t);
所以你可以这样做:
transform.position = Vector3.Lerp(_startLocation, _endLocation, t);
或者
transform.position = _currentLocation;
两者都会这样做,因为您已将_currentLocation分配给
Vector3.Lerp(_startLocation, _endLocation, t);
你可以在这里阅读有关MoveTowards的内容 http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html
答案 1 :(得分:0)
检查评论行您将了解我编辑的内容以提供解决方案。 它适用于我
void Start()
{
_startLocation = transform.position; // Changed Here to Initialize once
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Left mouse button clicked");
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.CompareTag("Ground"))
{
print(hit.point);
//**Here you mentioned _StartLocation = transform.position
//this instantly changes the starting point Every time so
//if SP changes then totally changed in Translation.***
_endLocation= hit.point;
_isMoving = true;
_startTime = Time.time;
_distanceToTravel = Vector3.Distance(_startLocation, _endLocation);
}
}
}
if (_isMoving)
{
Move();
}
}
void Move()
{
float timeElapsed = (Time.time - _startTime) * Speed;
float t = timeElapsed / _distanceToTravel;
_currentLocation = Vector3.Lerp(_startLocation, _endLocation, t);
transform.position = Vector3.Lerp(_startLocation, _endLocation, t);
_startLocation = _endLocation;
//*** This line is for Next Mouse
//click. So every next click StartPoint is Current Click
//EndPoint***
if (_currentLocation == _endLocation)
{
Debug.Log(string.Format("Destination reached ({0})", _endLocation.ToString()));
_isMoving = false;
}
}
}