'How to change keyboard control to touch control ? unity2d
If so, then this code is not mine, since I am still a beginner but trying to create my own game. How to add touch control to a script? I tried to look for other scripts, write my own, but there were always errors or the code did not do what I expected from it. Thanks in advance ;)
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody2D))]
public class playerMove : MonoBehaviour
{
public enum ProjectAxis { onlyX = 0, xAndY = 1 };
public ProjectAxis projectAxis = ProjectAxis.onlyX;
public float speed = 150;
public float addForce = 7;
public bool lookAtCursor;
public KeyCode leftButton = KeyCode.A;
public KeyCode rightButton = KeyCode.D;
public KeyCode upButton = KeyCode.W;
public KeyCode downButton = KeyCode.S;
public KeyCode addForceButton = KeyCode.Space;
public bool isFacingRight = true;
private Vector3 direction;
private float vertical;
private float horizontal;
private Rigidbody2D body;
private float rotationY;
private bool jump;
void Start()
{
body = GetComponent<Rigidbody2D>();
body.fixedAngle = true;
if (projectAxis == ProjectAxis.xAndY)
{
body.gravityScale = 0;
body.drag = 10;
}
}
void OnCollisionStay2D(Collision2D coll)
{
if (coll.transform.tag == "Ground")
{
body.drag = 10;
jump = true;
}
}
void OnCollisionExit2D(Collision2D coll)
{
if (coll.transform.tag == "Ground")
{
body.drag = 0;
jump = false;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
