- Home /
Question by
nasby321 · Dec 02, 2014 at 08:37 PM ·
2dtouchscreentabletfantasy
I need help setting up touch controls.
I need help making touch controls for my 2D sidescroller.
I have tried several youtube and text tutorials, but they don't show you everything you need. They are also in javascript. I use C#.
Attached I have arrow textures that I want for movement, but I don't know how to get them to appear on screen and to follow the character/scene when moving. The attacking can be activated by clicking anywhere on the tablet or phone.
using UnityEngine;
using System.Collections;
using SpriteFactory;
public class player : MonoBehaviour {
public int maxXPosition = 50;
private Vector3 initialPos;
private Quaternion initialRotation;
public int points = 0;
public float speed = 9.0f;
public float jumpSpeed = 300.0f;
public float gravity = 10.0f;
public bool grounded = true;
float height;
int layerMask;
public int numberOfPointsToWin = 30;
public int die = -1;
public bool charPressed = false;
private Transform thisTransform;
private Sprite sprite;
private bool flipped;
IEnumerator Death() {
yield return new WaitForSeconds(0.1f);
PointCounter.lives -= 1;
transform.rotation = initialRotation;
transform.position = initialPos;
}
// Use this for initialization
void Awake () {
thisTransform = transform;
sprite = (Sprite)GetComponent(typeof(Sprite));
height = collider.bounds.extents.y;
layerMask = LayerMask.NameToLayer("Player");
}
void Start () {
initialPos = transform.position;
initialRotation = transform.rotation;
}
// Update is called once per frame
void Update () {
Vector3 moveVector = new Vector3(0,0,0);
bool moving = false;
moveVector.y -= gravity * Time.deltaTime;
if(!Physics.Raycast(thisTransform.position, Vector3.down, height,layerMask)){
sprite.Play("jump");
}
if(Input.GetKeyDown(KeyCode.Space)){
Jump();
}
if(Input.GetKey(KeyCode.A)){
moveVector.x -= 1.0f;
moving = true;
}
if(Input.GetKey(KeyCode.D)){
moveVector.x += 1.0f;
moving = true;
}
if(Input.GetKey(KeyCode.F)){
Attack();
} else {
if(!IsAttacking()) {
if(moving) {
Move(moveVector);
} else {
Stand();
}
}
}
}
private void Stand() {
sprite.Stop();
}
private void Move(Vector3 moveVector) {
FlipSprite(moveVector.x);
moveVector *= speed * Time.deltaTime;
thisTransform.position = thisTransform.position + moveVector;
sprite.Play("walk");
}
private void FlipSprite(float x) {
if(x == 0.0f) return;
float xDir = Mathf.Sign(x);
if(!flipped && xDir < 0.0f) {
flipped = true;
sprite.SetFlippedState(true, false);
} else if(flipped&& xDir > 0.0f) {
flipped = false;
sprite.SetFlippedState(false, false);
}
}
private void Attack() {
if(IsAttacking()) return;
sprite.Play("Sword");
}
private bool IsAttacking() {
if(sprite.IsAnimationPlaying("Sword")){ return false;}
return false;
}
void Jump(){
if(grounded == true)
{
rigidbody.AddForce(Vector3.up * jumpSpeed);
grounded = false;
}
}
void Gravity(){
if(grounded == true)
{
rigidbody.AddForce(Vector3.up * jumpSpeed);
grounded = false;
}
}
void OnCollisionStay(Collision col){
if(col.gameObject.tag == "floor"){
grounded = true;
}
}
void OnTriggerEnter(Collider other) {
if(other.gameObject.name == "Enemy"){
sprite.Play("hurtt");
if(sprite.IsAnimationPlaying ("hurtt")){
StartCoroutine(Death());
}
}
}
void OnTriggerExit(Collider other) {
}
}
right2.png
(18.4 kB)
left2.png
(18.4 kB)
Comment
Your answer
Follow this Question
Related Questions
[2D] Click/touch handler for both Mobile and PC 0 Answers
[SOLVED]2D lookAt is off, any ideas? 1 Answer
2D rotating dial 1 Answer
Find distance within screen! 2 Answers
Trying to make my 2D Character Controller stick to a wall. 0 Answers