- Home /
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
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);
}
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.
i do as you say but the functions didn't appear in the button OnClick
i do as you say but the functions didn't appear in the button OnClick
@RodrigoAbreu i do as you say but the functions didn't appear in the button OnClick
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.
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.