我尝试编写一个脚本,用手指显示后面的精灵。但我需要这个精灵只能在离锚的指定距离上移动,并且只能在指定的角度之间移动。现在,我有下一个代码:
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);
}
}
我尝试在差异target
上旋转向量_min|maxAngle - angle
,但它的工作错误。如何制作?
目前的问题:
附:我重试了很多变种,但冷却不成功。如果它需要一些细节,请写信给我,我会发布。