I am trying to figure out how to implement touchscreen for android. I want to be able to tap the left hand of the screen to go left while achieving the same type of movement I already have implemented through keyboard commands and tap the right side of the screen to go right with the same type of movement I already have through keyboard commands.
The code I am using for player movement is:
using UnityEngine;
using System.Collections;
public class movement : MonoBehaviour
{
public Vector2 leftForce = new Vector2(-5, 5);
public Vector2 rightForce = new Vector2(5, 5);
// Update is called once per frame
void Update()
{
// Jump Left
if (Input.GetKeyDown("a"))
{
GetComponent<Rigidbody2D>().AddForce(leftForce, ForceMode2D.Impulse);
}
//Jump Right
if (Input.GetKeyDown("d"))
{
GetComponent<Rigidbody2D>().AddForce(rightForce, ForceMode2D.Impulse);
}
}
}
The code I have for movement is very simple, but I haven’t been able to find the right tutorial that explains how to add touch functionality that uses this same type of code.
Thank you all for your time!
EDIT: If anyone needs any clarification please let me know. I am new to game programming and I figured I would start with something easy like a 2D game with VERY simple controls. I have a basic working model of the game on PC (without menus, point counters, etc), but I designed it for android… and being the brilliant newbie I am… I didn’t take into consideration that I would have to create new touch controls for the game.
I have done a lot of tutorials (I have spent two days… at least 8 hours on this), but none of them cover how to adapt my controls to a working touch interface. I have learned how to apply the CrossPlatformInput assets to my game, but it doesn’t let me keep my movement script setup.
I have tried to setup my own UI Butttons, but I can’t figure out how to make my script be the trigger for the buttons when pressed. I have changed the classes in my script to public, but I think I am so lost at this point that I really need someone to write the script with mine in mind and then help me by explaining each part.
Again, thank you all for at least reading my question. I really hope someone out there can take pity on this poor “little” (I am 36 years old… ancient) unity hobby noob.