• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by vbvbnbbbj · Feb 04, 2021 at 09:16 AM · uibutton2d character

Make UI button or Joystick Problem

Hi,


I'm new to unity and have problem with making UI buttons or joystick to move left and right and jump. Can anyone help pls??


I have 2d Character. I put this two scripts in it


How I can modify them to make the 2d character move with UI buttons ?

*Note I use unity 5.6


First one

 public class PlayerPlatformerController : PhysicsObject {
 public float maxSpeed = 7;
 public float jumpTakeOffSpeed = 7;

 private SpriteRenderer spriteRenderer;
 private Animator animator;

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


The second one

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


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KarynaVlad · 3 days ago

Hi, @vbvbnbbbj , now i am study course https://www.html.it/pag/364391/unity-platform-2d-creare-personaggio/ But when i copy create 2 script and copy code, i have 31 errors. My version: First class = class Move:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Move: 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 class = PhysicsObject:

 //Script PhysicsObject
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 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 hitBufferList = new List(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)
         {
             hitBufferList.Clear();
             int count = rb2d.Cast(move, contactFilter, hitBuffer, distance + shellRadius);
             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;
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

253 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What have I done wrong on the OnClick script from Creating a Main Menu tutorial? 0 Answers

How do I press an UI button with Gamepad controls? 0 Answers

Slide-Out Panel Animates on Button - How to Animate on Swipe Motion on Mobile? 1 Answer

is it possible to have two on click events in one button 1 Answer

UI Button OnClick Only Works the First Time 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges