- Home /
I cant figure out how to change my simple Movement controls to touch, can anyone help?
Hello, I need some help on how to change this movement script of mine to touch controls if possible. I'm extremely new to this and have been looking all over for how to do this but it just doesn't make sense to me. If you could accomplish touch controls without changing much that would be awesome because the script is put onto the player and has restrictions that only work the way I currently have it setup. Here is what I have:
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
float lerpTime;
float currentlerpTime;
float perc = 1;
Vector3 startPos;
Vector3 endPos;
public float minX = 0;
public float maxX = 2;
public float minZ = 0;
public float maxZ = 1;
bool firstInput;
public bool justJump;
void Update ()
{
if (Input.GetButtonDown ("up") || Input.GetButtonDown ("down") || Input.GetButtonDown ("left") || Input.GetButtonDown ("right"))
{
if(perc == 1)
{
lerpTime = 1;
currentlerpTime = 0;
firstInput = true;
justJump = true;
}
}
startPos = gameObject.transform.position;
if(Input.GetButtonDown("right") && gameObject.transform.position == endPos && transform.position.x < maxX)
{
endPos = new Vector3(transform.position.x + 1,transform.position.y,transform.position.z);
}
if(Input.GetButtonDown("left") && gameObject.transform.position == endPos && transform.position.x > minX)
{
endPos = new Vector3(transform.position.x - 1,transform.position.y,transform.position.z);
}
if(Input.GetButtonDown("up") && gameObject.transform.position == endPos && transform.position.z < maxZ)
{
endPos = new Vector3(transform.position.x,transform.position.y,transform.position.z + 1);
}
if(Input.GetButtonDown("down") && gameObject.transform.position == endPos && transform.position.z > minZ)
{
endPos = new Vector3(transform.position.x,transform.position.y,transform.position.z - 1);
}
if (firstInput == true) {
currentlerpTime += Time.deltaTime * 5;
perc = currentlerpTime / lerpTime;
gameObject.transform.position = Vector3.Lerp (startPos, endPos, perc);
if (perc > 0.8F) {
perc = 1;
}
if (Mathf.Round (perc) == 1) {
justJump = false;
}
}
}
}
For people that might stumble into this question, I have figured out how to accomplish my problem and I no longer need any help. Thanks.
Answer by Meltdown · May 09, 2015 at 07:44 AM
Nobody is going to write your scripts for you, I would suggest learning how to build touch from scratch, there are hundreds of tutorials on the internet that explain how Unity and touch works, it's important when you are writing code that you understand what is going on.
Having someone write it for you will just make things even more difficult down the road when you need change things.
Take a look at this video tutorial
Hello and thank you for the reply. I realize after reading this question again that I came off as if I wanted some to write the code for me. I never intended to have someone do this for me as I really want to learn. Thank you for the video tutorial and the advise you gave me.
Your answer
Follow this Question
Related Questions
NPC movement 1 Answer
How can I make my characters movement better? 1 Answer
How to drag object along with dragging touch? 1 Answer
Help with iphone/android touch controls 2 Answers
Touch movement not working 1 Answer