Touch Controls 2d platformer.
Hello. So i have my movement script working but i cant seam to implement it to my buttons. Can someone help me?
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditorInternal;
public class PlayerController : MonoBehaviour {
//movement variables
public float maxSpeed;
//jumping variables
bool grounded = false;
float groundCheckRadius = 0.2f;
public LayerMask groundLayer;
public Transform groundCheck;
public float jumpHeight;
Rigidbody2D myRB;
Animator myAnim;
bool facingRight;
// Use this for initialization
void Start () {
myRB = GetComponent<Rigidbody2D>();
myAnim = GetComponent<Animator>();
facingRight = true;
}
// Update is called once per frame
void Update()
{
if UNITY_STANDALONE || UNITY_WEBPLAYER
if (grounded && Input.GetAxis("Jump") > 0)
{
Jump();
}
endif
} void FixedUpdate () { #if UNITY_STANDALONE || UNITY_WEBPLAYER
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
myAnim.SetBool("isGrounded", grounded);
myAnim.SetFloat("verticalSpeed", myRB.velocity.y);
move2(Input.GetAxis("Horizontal"));
endif
}
void flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
public void move2(float move)
{
myAnim.SetFloat("speed", Mathf.Abs(move));
myRB.velocity = new Vector2(move * maxSpeed, myRB.velocity.y);
if (move > 0 && !facingRight)
{
flip();
}
else if (move < 0 && facingRight)
{
flip();
}
}
public void Jump()
{
grounded = false;
myAnim.SetBool("isGrounded", grounded);
myRB.AddForce(new Vector2(0, jumpHeight));
}
}
honestly, I don't understand what you're in need of. Take some time and tell us whats going right, and whats going wrong. Format you're code so it's not a blast of stuff and then we can help.
Your answer
Follow this Question
Related Questions
mobile shooting animation not playing on pointerDown 1 Answer
Could someone please help with 2D touch controls? 0 Answers
Implementation of control TouchScreen 0 Answers
Problem With a Multi-Jump C# script, robot 2d character controller 1 Answer
2D Platformer, Rotate sprite/arm towards cursor not working [C] 0 Answers