- Home /
Question by
Saeid_Namdarii · Nov 04, 2021 at 07:15 AM ·
androidmovementinputballroll a ball
how to rolle a ball by touch or mouse
hello , i cant finde a toutrial how to move and roll a ball in android Pleas helpe me , thanks ♥ and i have code but i cant roll a ball;
private Vector2 lastmousePostion;
private GameObject currentLevel;
private bool MoveByTouch;
private Vector3 _mousStartPos, PlayerStartPos;
public float maxAcceleration;
private Rigidbody rb;
public float speedtourge = 5f;
private void Start()
{
maxAcceleration = 10f;
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
MoveByTouch = true;
Plane plane = new Plane(Vector3.up * Time.deltaTime, 0f);
float Distance;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out Distance))
{
_mousStartPos = ray.GetPoint(Distance);
PlayerStartPos = transform.position;
}
}
else if (Input.GetMouseButtonUp(0))
{
MoveByTouch = false;
}
if (MoveByTouch)
{
Plane plane = new Plane(Vector3.up * Time.deltaTime, 0f);
float Distance;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out Distance))
{
Vector3 MousePos = ray.GetPoint(Distance);
Vector3 move = MousePos - _mousStartPos;
Vector3 Controller = PlayerStartPos + move;
Controller.x = Mathf.Clamp(Controller.x, -8.6f, 8.6f);
transform.position = Vector3.Lerp(transform.position, new Vector3(Controller.x, transform.position.y, transform.position.z), Time.deltaTime * maxAcceleration);
rb.AddTorque(transform.up * speedtourge * Controller.x);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613044952im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to make ball rolling forward while follows a curve track? 0 Answers
Getting My Character to Move 1 Answer
Input lag on Andorid but not in Unity 1 Answer
Facing the players game object (upward, downward, left, right) on Android phone using joystick 0 Answers
Help with touchscreen controls 2 Answers