Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by vbvbnbbbj · Feb 07, 2021 at 01:06 PM · unity 5scripting problembutton

Problem moving Rigidbody with UI buttons

I'm new to unity , i have 2d character and 3 buttons right , left and jump buttons and 2 scripts i have attached to 2d character and I'm using 2DPlatformerController scripts from this ([Recorded Video Session: 2D Platformer Character Controller][1]) and i modified scripts to move with buttons ui and linked the button to the script in character but it doesn't work what is the problem pls this the first script

 public class PlayerPlatformerController : PhysicsObject {
 
     public float maxSpeed = 7;
     public float jumpTakeOffSpeed = 7;
 
     private SpriteRenderer spriteRenderer;
     private Animator animator;
 
 
     public Button jumpButton;
     public Button rightButton;
     public Button leftButton;
 
 
     public float jumpSpeed;
     public float speed;
 
     
     // Use this for initialization
     private void Awake () 
     {
         spriteRenderer = GetComponent<SpriteRenderer> ();    
         animator = GetComponent<Animator> ();
 
         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);
 }
 
     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;
     }
 }
   

and this the 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;
      }
  
  }


note*i'm using unity5.6

and this photo for the script in the character ![script][2] and another for the button ![button][3]

Update

i must assign the buttons OnClick but i don't know how [1]: https://learn.unity.com/tutorial/live-session-2d-platformer-character-controller [2]: https://i.stack.imgur.com/ZLbkV.png [3]: https://i.stack.imgur.com/zwEoe.png

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by RodrigoAbreu · Feb 07, 2021 at 05:56 PM

Yes, you did it here:

 // Use this for initialization
 private void Awake () 
 {
     spriteRenderer = GetComponent<SpriteRenderer> ();    
     animator = GetComponent<Animator> ();
 
     jumpButton.onClick.AddListener(Jump);
     rightButton.onClick.AddListener(MoveRight);
     leftButton.onClick.AddListener(MoveLeft);
 }


Something you could do is to stop calling GetComponent for the RigidBody2d. You have it already cached in your base class and it's protected


 private void Jump()
 {
     if(grounded)
     {
         rb2d.AddForce(Vector2.up * jumpSpeed);
     }
 }
 
 private void MoveRight()
 {
     rb2d.AddForce(Vector2.right * speed);
 }
 
 private void MoveLeft()
 {
     rb2d.AddForce(Vector2.left * speed);
 }

Comment
Add comment · Show 20 · 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
avatar image vbvbnbbbj · Feb 07, 2021 at 05:58 PM 0
Share

what should i do??

avatar image RodrigoAbreu vbvbnbbbj · Feb 07, 2021 at 06:10 PM 0
Share

Your scripts work for me, maybe it's your collider and rigidbody. What values are you passing as JumpSpeed and Speed to your PlayerPlatformerController? $$anonymous$$aybe try both as 100.

avatar image vbvbnbbbj RodrigoAbreu · Feb 07, 2021 at 06:11 PM 0
Share

i do as you say but the functions didn't appear in the button OnClick

Show more comments
avatar image vbvbnbbbj · Feb 07, 2021 at 06:06 PM 0
Share

i do as you say but the functions didn't appear in the button OnClick

avatar image vbvbnbbbj · Feb 07, 2021 at 06:10 PM 0
Share

@RodrigoAbreu i do as you say but the functions didn't appear in the button OnClick

avatar image RodrigoAbreu vbvbnbbbj · Feb 07, 2021 at 06:18 PM 0
Share

When you assign OnClick from script it doesn't show up in the inspector of that button, it's normal. Also you don't have to assign both scripts to your player, all you need is the PlayerPlatformerController, and change those speed to 100, it should work.

avatar image vbvbnbbbj RodrigoAbreu · Feb 07, 2021 at 06:26 PM 0
Share

can you write the script ? @RodrigoAbreu

Show more comments
avatar image
0

Answer by logicandchaos · Feb 07, 2021 at 04:50 PM

You didn't assign the On Click on your button component. You click the plus, drag in the object with the script on it and select the function.

Comment
Add comment · Show 1 · 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
avatar image vbvbnbbbj · Feb 07, 2021 at 04:54 PM 0
Share

there are many options what should i use?

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

302 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 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

Why there is an 'SerializationException: serializationStream supports seeking, but its length is 0' error when I click the load button? 2 Answers

Reference DLL's 1 Answer

How to detect an object which be in FOV of certain camera ? 1 Answer

How do I control the behavior of Android soft buttons (Immersive mode)? 1 Answer

How to make Input.GetAxis work on certain area only? 0 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