2015-09-17 25 views
0

这里是我的代码:简单的点击移动脚本不起作用?

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功能,并结束了这段代码。

调试控制台确认Ground已经被击中,但是我的胶囊开始在Y方向向上移动并且永不停止!

我对Unity和一般的游戏开发还很陌生,所以我还在学习,但是我做错了什么?

+0

我想我只是意识到D,我使用'deltaTime'时,实际上应该保持时间,因为它是一个线性函数,因为它是一个线性函数... –

+0

我编辑了我的代码以正确使用'Lerp'函数(而不是使用deltaTime ',但是我仍然应该在那里使用...?),所以现在我的胶囊在x轴和y轴上移动,但它仍然无限地在Z轴上向上移动。 –

+0

更正:在正Y轴上无限移动,而不是Z.我习惯了Blender使用的坐标...... –

回答

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; 
    } 
} 
} 
1

它在Y移动,因为你使用transform.Translate。

transform.Translate移动,所以如果你没有transform.Translate(0,0,10) 它会在z移动,如果你没有transform.Translate(0,10,10) 它会沿y和z方向移动。

因此,要解决这个问题,我会告诉你2种方式:

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

+0

还有一件事重新初始化'_startLocation',将胶囊从最后一个位置移出 –

+0

是的,但我认为他想要和现在一样的行为,所以我只考虑替换transform.Translate :-)但是不管怎么说,还是要谢谢你 :-) – Jesper