Unity2d-Android-C#: How to convert current movement script to be used for touchscreen?
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.
Answer by Naphier · Nov 14, 2016 at 09:57 PM
Age matters not my friend! You are nowhere near ancient! There are many ways to go. UI Buttons are a good choice. To get a button to fire a method you'll need to create a class that inherits from MonoBehaviour, attach that script to some object in your scene, then in the button component you'll add an OnClick event. Then you drag/drop the gameObject with the class you created and select the correct method from the dropdown list.
The tutorials are the perfect place to learn how to do these things. For UI: https://unity3d.com/learn/tutorials/topics/user-interface-ui You may specifically want to watch the one for Buttons.
If you're interested in one-on-one tutoring, then check out my site (naplandgames.com). I've taught students from age 16 to 50. I do highly suggest working through some tutorials first, though!
Cheers and best of luck!