2D platformer script for Android

Hello, I’m new in the forum and a beginner in Unity. I have a Player Script from Youtube but I will convert it for Android. Can Anyone help me?(Sry for my bad englisch, I’m from Austria and 15 years old)
Here is the Script:

using UnityEngine;
using System.Collections;

public class Spikes : MonoBehaviour {

private Player player;

void Start(){

player = GameObject.FindGameObjectWithTag(“Player”).GetComponent();

}

void OnTriggerEnter2D(Collider2D col){

if(col.CompareTag(“Player”)){

player.Damage(1);

StartCoroutine(player.KnockBack(0.02f, -40, player.transform.position));

}

}

}

Oops false script sry, Here is the right:
using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

//Floats
public float maxSpeed = 3;
public float speed = 50f;
public float jumpPower = 150f;

//Booleans
public bool grounded;
public bool canDoubleJump;

//Stats
public int curHealth;
public int maxHealth = 3;

//References
private Rigidbody2D rb2d;
private Animator anim;

void Start ()
{
rb2d = gameObject.GetComponent();
anim = gameObject.GetComponent();

curHealth = maxHealth;

}

void Update ()
{

anim.SetBool(“Grounded”,grounded);
anim.SetFloat(“Speed”, Mathf.Abs(rb2d.velocity.x));

if (Input.GetAxis(“Horizontal”) < -0.1f)
{
transform.localScale = new Vector3(-1, 1, 1);
}

if (Input.GetAxis(“Horizontal”) > 0.1f)
{
transform.localScale = new Vector3(1, 1, 1);
}

if (Input.GetButtonDown(“Jump”))
{
if (grounded && Input.GetButtonDown(“Jump”))
{
rb2d.AddForce(Vector2.up * jumpPower);
grounded = false;
}
else
{

if (canDoubleJump)
{

canDoubleJump = false;
rb2d.velocity = new Vector2(rb2d.velocity.x, 0);
rb2d.AddForce(Vector2.up * jumpPower / 1.75f);

}

}
}

if(curHealth > maxHealth){
curHealth = maxHealth;
}

if(curHealth <= 0){

curHealth = 0;

Die ();

}

}

void FixedUpdate()
{
{

}
Vector3 easeVelocity = rb2d.velocity;
easeVelocity.y = rb2d.velocity.y;
easeVelocity.z = 0.0f;
easeVelocity.x *= 0.75f;

float h = Input.GetAxis(“Horizontal”);

//Fake friction / Easing the x speed of our player
if (grounded)
{

rb2d.velocity = easeVelocity;

}

//Moving the player
rb2d.AddForce((Vector2.right * speed) * h);

//Limiting the speed of the player
if (rb2d.velocity.x > maxSpeed)
{
rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
}

if (rb2d.velocity.x < -maxSpeed)
{
rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
}

}

void Die(){

Application.LoadLevel(Application.loadedLevel);

}

public void Damage(int dmg){

curHealth -= dmg;
gameObject.GetComponent().Play(“RedFlash1”);

}

public IEnumerator KnockBack (float knockDur, float knockBackPwr, Vector3 knockBackDir)
{

float timer = 0;
while (knockDur>timer) {
timer += Time.deltaTime;
rb2d.velocity = new Vector2 (0, 0);
rb2d.AddForce (new Vector3 (knockBackDir.x * -10, knockBackDir.y * knockBackPwr, transform.position.z));

}
yield return 0;

}

}

if this script works on pc, then it should work on android too.

Yes but if I make Buttons how can I make that if I touch the right Button, then the player moves right, if I touch the Jump button, the player jumps?

try import crossplatforminput from standart asset. So you can use most of standart pc moving scripts.

or check the tutorial for your own input
http://imakeinternet.com/unity-2d-tutorial-screen-player-touch-controller/
(this is little old tutorial, but it works for touches on android. For gui texture, make empty object and add rendering ->gui texture (may be you can see gui texture only in game tab, so switch from editor and adjust it). For rigidbody2d add in script:
Rigidbody2D rigidbody2D;
{
rigidbody2D = GetComponent ();
}

Thank you, where should I add it? In start, update, fixed Update?

Rigidbody2D rigidbody2D;
void Start () {
rigidbody2D = GetComponent ();
}

Thank you but what should i write in the script?

Hi, you should really start with the tutorials so you can learn. Getting help from scripts on the internet is fine but you will need an understanding so you can make it work for your game.

Start at the beginning with the roll a ball tutorial

https://unity3d.com/learn/tutorials

1 Like

in old unity tutorials (befor 4.6), it was possible directly using of rigidbody2D (rigidbody2D.velocity). Now you should make reference to it (
Rigidbody2D rigidbody2D;
void Start () {
rigidbody2D = GetComponent ();
}
). So if you make the tutorial for android touches that i have linked, so just add:
Line 12: Rigidbody2D rigidbody2D;
Line 20:
void Start () {
rigidbody2D = GetComponent ();
}

or you can just try to use the old tutorial, i think unity can adjust it automaticly.