我是 unity 的新手,在制作 UI 按钮左右移动和跳跃时遇到问题任何人都可以帮忙吗?? 我正在使用 2DPlatformerController Recorded Video Session: 2D Platformer Character Controller 我有 2d 角色我把这两个脚本放在里面 首先
public class PlayerPlatformerController : PhysicsObject {
public float maxSpeed = 7;
public float jumpTakeOffSpeed = 7;
private SpriteRenderer spriteRenderer;
private Animator animator;
// Use this for initialization
void Awake ()
{
spriteRenderer = GetComponent<SpriteRenderer> ();
animator = GetComponent<Animator> ();
}
protected override void ComputeVelocity()
{
Vector2 move = Vector2.zero;
move.x = Input.GetAxis ("Horizontal");
if (Input.GetButtonDown ("Jump") && grounded) {
velocity.y = jumpTakeOffSpeed;
} else if (Input.GetButtonUp ("Jump"))
{
if (velocity.y > 0) {
velocity.y = velocity.y * 0.5f;
}
}
bool flipSprite = (spriteRenderer.flipX ? (move.x > 0.01f) : (move.x < 0.01f));
if (flipSprite)
{
spriteRenderer.flipX = !spriteRenderer.flipX;
}
animator.SetBool ("grounded", grounded);
animator.SetFloat ("velocityX", Mathf.Abs (velocity.x) / maxSpeed);
targetVelocity = move * maxSpeed;
}
}
第二个
public class PhysicsObject : MonoBehaviour {
public float minGroundNormalY = .65f;
public float gravityModifier = 1f;
protected Vector2 targetVelocity;
protected bool grounded;
protected Vector2 groundNormal;
protected Rigidbody2D rb2d;
protected Vector2 velocity;
protected ContactFilter2D contactFilter;
protected RaycastHit2D[] hitBuffer = new RaycastHit2D[16];
protected List<RaycastHit2D> hitBufferList = new List<RaycastHit2D> (16);
protected const float minMoveDistance = 0.001f;
protected const float shellRadius = 0.01f;
void OnEnable()
{
rb2d = GetComponent<Rigidbody2D> ();
}
void Start ()
{
contactFilter.useTriggers = false;
contactFilter.SetLayerMask (Physics2D.GetLayerCollisionMask (gameObject.layer));
contactFilter.useLayerMask = true;
}
void Update ()
{
targetVelocity = Vector2.zero;
ComputeVelocity ();
}
protected virtual void ComputeVelocity()
{
}
void FixedUpdate()
{
velocity += gravityModifier * Physics2D.gravity * Time.deltaTime;
velocity.x = targetVelocity.x;
grounded = false;
Vector2 deltaPosition = velocity * Time.deltaTime;
Vector2 moveAlongGround = new Vector2 (groundNormal.y, -groundNormal.x);
Vector2 move = moveAlongGround * deltaPosition.x;
Movement (move, false);
move = Vector2.up * deltaPosition.y;
Movement (move, true);
}
void Movement(Vector2 move, bool yMovement)
{
float distance = move.magnitude;
if (distance > minMoveDistance)
{
int count = rb2d.Cast (move, contactFilter, hitBuffer, distance + shellRadius);
hitBufferList.Clear ();
for (int i = 0; i < count; i++) {
hitBufferList.Add (hitBuffer [i]);
}
for (int i = 0; i < hitBufferList.Count; i++)
{
Vector2 currentNormal = hitBufferList [i].normal;
if (currentNormal.y > minGroundNormalY)
{
grounded = true;
if (yMovement)
{
groundNormal = currentNormal;
currentNormal.x = 0;
}
}
float projection = Vector2.Dot (velocity, currentNormal);
if (projection < 0)
{
velocity = velocity - projection * currentNormal;
}
float modifiedDistance = hitBufferList [i].distance - shellRadius;
distance = modifiedDistance < distance ? modifiedDistance : distance;
}
}
rb2d.position = rb2d.position + move.normalized * distance;
}
}
如何修改脚本以使二维角色随 UI 按钮移动? *注意我使用的是 unity 5.6
答案 0 :(得分:0)
有多种处理用户输入的方法,实际上在新的 Unity 版本上有一个全新的如何处理输入系统的版本,您可以查看它here。
一个问题是,您是想要一个操纵杆来移动它(如 PS/Xbox 控制器)还是像您提议的那样在每个方向使用一个按钮。
对于操纵杆解决方案,我建议您使用 StandardAssets 插件中的 CrossPlatformInput 插件,知道您使用的是较低版本的 Unity,它会很有用。
使用起来很简单,例如:
m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
但是,如果你想用 3 个按钮来做,你必须为这些按钮制作 UI,然后让听者知道按下了哪个按钮,并将各自的力施加到指定的方向上,比如:
public Button jumpButton;
public Button rightButton;
public Button leftButton;
public float jumpSpeed;
public float speed;
private void Awake()
{
jumpButton.onClick.AddListener(Jump);
rightButton.onClick.AddListener(MoveRight);
leftButton.onClick.AddListener(MoveLeft);
}
private void Jump()
{
if(grounded)
{
GetComponent<Rigidbody2D>().AddForce(Vector2.up * jumpSpeed);
}
}
private void MoveRight()
{
GetComponent<Rigidbody2D>().AddForce(Vector2.right * speed);
}
private void MoveLeft()
{
GetComponent<Rigidbody2D>().AddForce(Vector2.left * speed);
}
注意:我现在没有 Unity 来 100% 测试它,所以如果有任何语法错误,请告诉我修复答案,请^^