0
\$\begingroup\$

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; 
}
\$\endgroup\$
0
\$\begingroup\$

2 Problems:
First: Your question, too short, vague. I can't be sure what you are talking about.
Second: What you want (if i understood your question) is probably not what you imagined. When you drag an object the camera is a child of (constrained to) you will either get invinite movement until you let go, or only short pulses of movement(when you reset your cursor to 0 relative position all the time).
Imagine this: you have your object. you press on it and move your mouse. Then you get the relative vector between the mouse position where you started pressing at and the current position. This vector you multiply by speed (and Time.deltaTime and some screen independent paramters) to make a speed vector for the object to move at. the object moves.
however now your mouse is still on a relative position, thus resulting you to move every single frame.
I suggest you make clear what exactly you are trying to do.

\$\endgroup\$
  • \$\begingroup\$ thank you for your response and sorry I didn't provide full information. I've edited the question and also added my ball move code and camera follow code. I hope this will help. \$\endgroup\$ – Abhinay Singh Negi Apr 26 '18 at 14:14
  • \$\begingroup\$ If something continues to move even though you pressed just once it means, that either your code does eaxctly that or you forgot to clean the memory that specifies that the specific action is to be done. I suggest you print (Debug.Log) the speed and what the mouse state is. This will help you. I guarantee it. I can't look into it any further though at the moment \$\endgroup\$ – RIJIK Apr 26 '18 at 23:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.