Hello all,
So I’m really new to unity and was building a little 2d side scroller game for my andriod phone but I started with getting controls working on the computer which was working out really well, but then I moved it over to the touch controls so it would work on my Andriod phone and this is where things fell apart.
The way I want the controls to work is if the user touches the left or right side of the screen it will just move the character in that direction. The next part of the controls I want working is if the player touches the screen with another figure the character will just jump in the direction it was moving. So here is another example: I would have the phone in my hand sideways like a controller, if my right thumb touches the screen on the right side the character will move in that direction, If I however press the screen anywhere with my left thumb (touch count >1) the character will simply jump in the direction it was going. vice a versa if i’m going in the other direction.
I have tried my hardest to get this to work but it just isn’t working. Here is my code so far:
using UnityEngine;
using System.Collections;
public class KittenControllerv4 : MonoBehaviour
{
public float MaxSpeed = 2f;
bool facingRight = true;
Animator anim;
//check for ground variables
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 700f;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void FixedUpdate ()
{
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
if (Input.touchCount > 0)
{
Touch touchtrack = Input.GetTouch (0);
if (touchtrack.phase == TouchPhase.Stationary || touchtrack.phase == TouchPhase.Moved)
{
float spritePos = transform.position.x;
if (touchtrack.position.x > Screen.width/2)
{
move = 1;
anim.SetFloat ("Speed", Mathf.Abs (move));
rigidbody2D.velocity = new Vector2 (move * MaxSpeed, rigidbody2D.velocity.y);
if (!facingRight)
Flip ();
}
else
{
move = -1;
anim.SetFloat ("Speed", Mathf.Abs (move));
rigidbody2D.velocity = new Vector2 (move * MaxSpeed, rigidbody2D.velocity.y);
if (facingRight)
Flip ();
}
}
}
else {
move = 0;
anim.SetFloat ("Speed", Mathf.Abs (move));
}
}
void Update()
{
if (grounded Input.touchCount > 1)
{
anim.SetBool ("Ground", false);
rigidbody2D.AddForce (new Vector2 (0, jumpForce));
return;
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
The only thing I can think of is I was using the rigidbody2d velocity to move the character when it was using keyboard controllers and I might have to go away from that? What currently happens with this code is the player character will run towards which ever way I touch on the screen but the jump always goes in the wrong direction.
Also if there is a best practice, or better way of doing this I have no issues changing the code to that as the screen.width/2 seems like a sloppy way of achieving the end state goal.
Anyhow any help would be greatly appropriated and if this needs to be moved to the 2D forums feel free to do so, I wasn’t sure which forum to post this in.
Thanks,