Hi. I’ve been developing a little android game,but the problem is i made my character with rigidbody and box and circle colliders. Now everywhere i find joysticks use character controllers,but they do not stack with rigidbody sadly =/ Is it possible to make joystick for rigidbody with box and circle colliders? And if yes,could anyone explain how? This is my character control script:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float maxSpeed = 10f;
bool facingRight = true;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 700f;
bool doubleJump = false;
Animator anim;
void Start () {
anim = GetComponent<Animator>();
}
void FixedUpdate () {
//Check if grounded.
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool("Ground", grounded);
if(grounded)
doubleJump = false;
anim.SetFloat("vSpeed", rigidbody2D.velocity.y);
float move = Input.GetAxis ("Horizontal"); //Controller
//Run animation.
anim.SetFloat("Speed", Mathf.Abs(move));
rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
if(move > 0 !facingRight) {
Flip();
} else if(move < 0 facingRight) {
Flip();
}
}
//Android jump button.
void OnGUI() {
if(GUI.Button(new Rect(Screen.width - 160,Screen.height - 130, 150, 120),"Jump!")) {
if((grounded || !doubleJump)) {
anim.SetBool("Ground", false);
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
rigidbody2D.AddForce(new Vector2(0, jumpForce));
if(!doubleJump !grounded) {
doubleJump = true;
}
}
}
}
//PC jump.
void Update() {
if((grounded || !doubleJump) Input.GetKeyDown(KeyCode.Space)) {
anim.SetBool("Ground", false);
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
rigidbody2D.AddForce(new Vector2(0, jumpForce));
if(!doubleJump !grounded) {
doubleJump = true;
}
}
}
//Face direction.
void Flip() {
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Thank you in advance
Cheers.