2016-07-24 3 views
2

私は指で続くスプライトを示すスクリプトを書こうとしています。しかし、私はこのスプライトが指定された角度の間だけ、アンカーから指定された距離だけ移動できる必要があります。私は違い_min|maxAngle - angle上のベクトルtargetを回転しようとするが、それは間違っている作品ベクトルの角度を制限する方法は?

public class GunPowerController : MonoBehaviour { 

    public GameObject _fingerprint; 
    public Transform _anchor; 
    public Gun _gun; 
    public float _maxPower = 1f; 
    public float _maxAngle = 15f; 
    public float _minAngle = 0f; 

    private Camera _camera; 
    private GameObject _fingerprintInstance; 

    void Awake() 
    { 
     _maxPower = Mathf.Abs(_maxPower); 
     _camera = Camera.main; 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     if (Input.GetMouseButtonDown(0)) 
     { 
      var touchWorldPosition = _camera.ScreenToWorldPoint(Input.mousePosition); 
      var hitInfo = Physics2D.Raycast(touchWorldPosition, Vector2.zero); 

      if (hitInfo && hitInfo.transform.gameObject.Equals(gameObject)) 
      { 
       touchWorldPosition.z = transform.position.z; 
       _fingerprintInstance = (GameObject)Instantiate(_fingerprint, touchWorldPosition, Quaternion.identity); 
      } 
     } 
     else if (_fingerprintInstance != null) 
     { 

      if (Input.GetMouseButtonUp(0)) 
      { 
       Destroy(_fingerprintInstance); 
      } 
      else 
      { 
       var touchWorldPosition = _camera.ScreenToWorldPoint(Input.mousePosition); 
       Move(touchWorldPosition); 
      } 
     } 
    } 

    private void Move(Vector3 target) 
    { 
     target.z = transform.position.z; 
     Vector3 distance = target - _anchor.position; 
     Vector3 axis = _anchor.position; 
     axis.x = -1f; 
     float angle = Vector3.Angle(axis, distance) * Mathf.Sign(distance.y - axis.y); 

     if (distance.sqrMagnitude > _maxPower * _maxPower) 
     { 
      distance.Normalize(); 
      distance *= _maxPower; 
      target = _anchor.position + distance; 
     } 

     if(_minAngle > angle) 
     { 
      //Here I need to hold vector rotation while the user doesn't 
      //return to the available space between angles. 
     } 
     else if (_maxAngle < angle) 
     { 
      //Here I need to hold vector rotation while the user doesn't 
      //return to the available space between angles. 
     } 

     _fingerprintInstance.transform.position = Vector3.Lerp(_fingerprintInstance.transform.position, target, 
      10f * Time.deltaTime); 
    } 
} 

:今、私は次のコードを持っています。それを作る方法?現在の問題: Scheme
P.S.私は多くの変種を再試行しましたが、冷たくはありません。それはいくつかの詳細が必要な場合は、pls、私は投稿します私を書いてください。

答えて

1

Mathf.Clampを使用してください。

リンク:https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html

+0

私は数字を制限する方法を知っています。私はベクトル角度のためにそれを作る方法を知らない –

+0

ベクトルの長さは常に変更することができます。 –

+1

Mathf.Clamp(transform.rotation.y、0、15); – ARTAGE

関連する問題