首先,我将总体解释游戏:项目中有几个相同的角色(跑步者),除第一个外,其他所有角色均处于非活动状态,当活动时(单击鼠标时角色运行,否则他们站立)触摸不活跃的人,他们开始和他一起跑步。 (加入碰撞参考)
问题 - 当任何活动角色触摸位于舞台两侧的场景的左墙或右墙时,如何关闭所有角色转向这面墙的能力(通过将鼠标向左移动到对)
带有角色移动和旋转的代码:
using UnityEngine;
public class StickmanMover : MonoBehaviour
{
[SerializeField] private float _speedMoving;
[SerializeField] private float _minRotation;
[SerializeField] private float _maxRotatioan;
[SerializeField] private float _maxSpeedMoving;
[SerializeField] private float _speedRotationSide;
[SerializeField] private float _speedRotationBack;
private bool _collidesBorder = false;
private Rigidbody _rigidbody;
private void Start()
{
_rigidbody = GetComponent<Rigidbody>();
}
private void Update()
{
if (Input.GetKey(KeyCode.Mouse0))
{
if (_rigidbody.velocity.magnitude < _maxSpeedMoving)
{
_rigidbody.AddForce(transform.forward * _speedMoving * Time.deltaTime);
}
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y + Input.GetAxis("Mouse X") * _speedRotationSide, 0);
LimitRotation();
}
if (!Input.GetKey(KeyCode.Mouse0))
{
SetDefaultRotation();
}
}
private void LimitRotation()
{
Vector3 stickmanRotation = transform.rotation.eulerAngles;
stickmanRotation.y = (stickmanRotation.y > 180) ? stickmanRotation.y - 360 : stickmanRotation.y;
stickmanRotation.y = Mathf.Clamp(stickmanRotation.y, _minRotation, _maxRotatioan);
transform.rotation = Quaternion.Euler(stickmanRotation);
}
private void SetDefaultRotation()
{
Quaternion rotation = Quaternion.LookRotation(Vector3.forward);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * _speedRotationBack);
}
}