Move a cube on android
Hi,
I have my code for moving a cube by touching and then drag it (on x axis) for Android but i dont know why, it works but not very well. It is difficult to control.
note: im new with unity...
using UnityEngine;
using System.Collections;
public class Paddle : MonoBehaviour
{
public float paddleSpeed = 1f;
private Vector3 playerPos = new Vector3(0f, -8.5f, 0f);
float pointer_x;
void Update()
{
pointer_x = Input.GetAxis("Horizontal");
if (Input.touchCount > 0)
pointer_x = Input.touches[0].deltaPosition.x;
float xPos = transform.position.x + (pointer_x * paddleSpeed);
playerPos = new Vector3(Mathf.Clamp(xPos, -7f, 7f), -8.5f, 0f);
transform.position = playerPos;
}
}
Comment
i wrote this one, but it doesnt work like i want
using UnityEngine;
using System.Collections;
public class Paddle : $$anonymous$$onoBehaviour
{
public float speed = 1f;
void Update()
{
float input;
if (Input.touchCount > 0) //touch input
input = Input.touches[0].position.x >= Screen.width / 2 ? 1f : -1f;
else //keyboard input
input = Input.GetAxis("Horizontal");
transform.Translate(new Vector2(input * speed, 0));
float currentX = $$anonymous$$athf.Clamp(transform.position.x, -7f, 7f);
transform.position = new Vector3(currentX, -8.5f, 0f);
}
}
Your answer
Follow this Question
Related Questions
Hello I have a problem with touches in Unity 2020.1 0 Answers
Simple touch counting program not working 0 Answers
How can i make my player look at the same direction where its moving? [JOYSTICK ANDROID] 0 Answers
rotating around by holding (android) 0 Answers
how to create moving foward character with button ? 0 Answers