Question by
CemGurbuz · Apr 20, 2019 at 07:06 AM ·
movementbuttonmobiletouchscreen
How to assign a key to the button
How to assign a key to the button.
I wrote these codes by looking at this video [ https://www.youtube.com/watch?v=VmGmMRhR-Ts&list=WL∈dex=80&t=0s ] but I don't understood the movement.
Please help me!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour {
//variables
public float moveSpeed = 300;
public GameObject character;
private Rigidbody2D characterBody;
private float ScreenWidth;
// Use this for initialization
void Start () {
ScreenWidth = Screen.width;
characterBody = character.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
int i = 0;
//loop over every touch found
while (i < Input.touchCount) {
if (Input.GetTouch (i).position.x > ScreenWidth / 2) {
//move right
RunCharacter (1.0f);
}
if (Input.GetTouch (i).position.x < ScreenWidth / 2) {
//move left
RunCharacter (-1.0f);
}
++i;
}
}
void FixedUpdate(){
#if UNITY_EDITOR
RunCharacter(Input.GetAxis("Horizontal"));
#endif
}
private void RunCharacter(float horizontalInput){
//move player
characterBody.AddForce(new Vector2(horizontalInput * moveSpeed * Time.deltaTime, 0));
}
}
ekran-goruntusu-41.png
(8.2 kB)
Comment
Can you be a bit more specific? What part of the movement don't you understand? What are you expecting and what results are you getting? What have you tried so far?