2015-09-17 27 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