简体   繁体   中英

I have problem with making ui buttons in unity

I'm new to unity and have problem with making UI buttons to move left and right and jump Any one can help pls?? I'm using 2DPlatformerController from this Recorded Video Session: 2D Platformer Character Controller I have 2d Character I put this two scripts in it First

    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;
     }
 }

second

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;
     }
 
 }

How I can modify the scripts to make the 2d character move with UI buttons? *Note I use unity 5.6

There are multiple ways to handle user inputs, in fact on the new Unity versions there's a whole new version of how to handle input system, you can check it here .

One question will be if you want a single joystick to move it arround (like a PS/Xbox controller) or use a single button for every direction, like you propouse.

For the joystick solution I'll advise you to use the CrossPlatformInput plugin inside the StandardAssets plugin, knowing that you are using a lower version of Unity, it will work like a charm.

It's simple to use like:

m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");

But, if you want to do it with 3 buttons, you will have to make the UI for the buttons, then enable the listener to know which one is pressed, and apply the respective force to the direction assign it, something like:

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);
}

Note: I don't have Unity right now to test it 100% so if there's any syntax error let me know to fix the answer please ^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM