I am working on Bowlingball game. And when I move the ball left and right by touching or clicking the camera moves, but it also makes ball keeps moving in the same direction even though I just moved the mouse once in that direction.
The code for moving ball left and right by touching and dragging.
void TouchInput()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = RayGenerator();
RaycastHit hit;
if (Physics.Raycast(ray.origin, ray.direction, out hit))
{
if (hit.transform.tag == "BowlingBall")
{
obj = hit.transform.gameObject;
objPlane = new Plane(Camera.main.transform.forward * -1, obj.transform.position);
Ray mRay = Camera.main.ScreenPointToRay(Input.mousePosition);
float distance = 0.0f;
objPlane.Raycast(mRay, out distance);
ballTouchOffset = obj.transform.position - mRay.GetPoint(distance);
}
}
} else if (Input.GetMouseButton(0) && obj)
{
Ray mRay = Camera.main.ScreenPointToRay(Input.mousePosition);
float distance;
if (objPlane.Raycast(mRay, out distance))
{
obj.transform.position = new Vector3(mRay.GetPoint(distance).x + ballTouchOffset.x, obj.transform.position.y, obj.transform.position.z);
}
} else if (Input.GetMouseButtonUp(0) & obj)
{
obj = null;
}
}
And the code for camera follow
public Transform bowlingBall;
public Vector3 offset;
private void LateUpdate()
{
transform.position = bowlingBall.position + offset;
}