HI guys, I am a newbie in using unity2D. I encounter some problems when I try to move the character with UI buttons. I have created two buttons to move right and left and generate some simple animation linkage using the animator controller.
However, when I play in the game scene, the character wont move according to the two buttons but only doing animation itself. Then I try to disable the animator attached on the character, although there are no animation, the buttons work which can control the movement of the character. I have created an empty game object called “frog_player” to hold all the sprites of the characters, and the scripts, collider and rigidbody are attached to the empty game object “frog_player”. I have worked for weeks but cannot look for the solution even the problem itself.
part of the files have been attached and really seek for any opinions to solve that, thank you .
The script of the player is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player_controller : MonoBehaviour
{
public float jumpforce;
public float speedX;
public Transform groundcheck;
public float checkradius;
public LayerMask whatisgrounded;
private bool facingright;
private bool isgrounded;
private float moveinput;
private bool jumping;
private Animator anim;
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
facingright = true;
}
// Update is called once per frame
void Update()
{
Moveplayer(moveinput);
isgrounded = Physics2D.OverlapCircle(groundcheck.position, checkradius, whatisgrounded);
if (isgrounded == true)
{
anim.SetBool("isJumping", false);
}
else
{
anim.SetBool("isJumping", true);
}
rb.velocity = new Vector3(moveinput, rb.velocity.y, 0);
}
void Moveplayer(float moveinput)
{
if (moveinput < 0 && !jumping || moveinput > 0 && !jumping)
{
anim.SetBool("isRunning", true);
flip();
}
if (moveinput == 0 && !jumping)
{
anim.SetBool("isRunning", false);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "Ground")
{
jumping = false;
anim.SetBool("isJumping", false);
}
}
void flip()
{
if (moveinput > 0 && !facingright || moveinput < 0 && facingright)
{
facingright = !facingright;
Vector3 scaler = transform.localScale;
scaler.x *= -1;
transform.localScale = scaler;
}
}
public void walkleft()
{
moveinput = -speedX;
}
public void walkright()
{
moveinput = speedX;
}
public void stopwalking()
{
moveinput = 0;
}
}
4862750–468260–testgame.unitypackage (474 KB)