0
\$\begingroup\$

I'm trying to get my coin object's Rigidbody to move whichever way it was swiped on by the user through RayCast

void Awake()
{
    coinRigidBody = coin.GetComponent<Rigidbody>();
}

void Update()
{

    foreach (Touch touch in Input.touches)
    {
        if (touch.phase == TouchPhase.Began)
        {
            firstTouchPos = new Vector3(touch.position.x, touch.position.y, Camera.main.nearClipPlane);
        }
        if (touch.phase == TouchPhase.Moved)
        {

            lastTouchPos = new Vector3(touch.position.x, touch.position.y, Camera.main.farClipPlane);
        }

        if (touch.phase == TouchPhase.Ended)
        {

            Vector3 firstTouchWorldPos = Camera.main.ScreenToWorldPoint(firstTouchPos);
            Vector3 lastTouchWorldPos = Camera.main.ScreenToWorldPoint(lastTouchPos);
            Vector3 diffWorldPos = lastTouchWorldPos - firstTouchWorldPos;
            direction3D = diffWorldPos.normalized;
            power = diffWorldPos.magnitude * (force / Time.deltaTime);

            Ray firstTouchRay = Camera.main.ScreenPointToRay(firstTouchPos);
            Ray lastTouchRay = Camera.main.ScreenPointToRay(lastTouchPos);

            RaycastHit hit;
            Debug.DrawRay(firstTouchRay.origin, lastTouchRay.direction - firstTouchRay.direction, Color.red, 50.0f);

            if (Physics.Raycast(firstTouchRay.origin, lastTouchRay.direction * 100, out hit))
            {
                Vector3 hitLocation = hit.point;
                if (hit.collider.tag == "Coin")
                {

                    projectedVector = Vector3.ProjectOnPlane(direction3D, plane.normal);
                    coinRigidBody.AddForceAtPosition(projectedVector * power, hitLocation * 5);
                    throwCoinObject.throwWithForce(power, hitLocation, direction3D);
                }
            }
        }
    }
}

The Code is working but my only problem is that it goes only in a certain direction? And it seems that direction is manipulated by how the camera is facing the objects. I just want it to hit the first touch origin then move to the direction the swipe was made.

Help would be really appreciated

\$\endgroup\$
0
\$\begingroup\$
  1. lastTouchRay.direction - firstTouchRay.direction does not do what you assume it does. ScreenPointToRay draws a ray from the camera to the point you specify. I'm assuming those directions are not what you want. The debug ray will be wrong.

  2. Your raycast uses the lastTouchRay direction, although it should use the first one if you're only interested in seeing if the first touch "touched" the coin.

  3. The Direction3D should be fine as is since both points are already on a plane ( z is nearClip for both ). You could get rid of the ProjectOnPlane. And in your case, you probably only need the z components to match so they end up as 0.0 in the Direction3D.

Hope this will at least put you on the right track:

if (touch.phase == TouchPhase.Ended) 
{

        Vector3 firstTouchWorldPos = Camera.main.ScreenToWorldPoint(firstTouchPos);
        Vector3 lastTouchWorldPos = Camera.main.ScreenToWorldPoint(lastTouchPos);
        Vector3 diffWorldPos = lastTouchWorldPos - firstTouchWorldPos;
        direction3D = diffWorldPos.normalized;
        power = diffWorldPos.magnitude *(force /Time.deltaTime);

        Ray firstTouchRay = Camera.main.ScreenPointToRay(firstTouchPos);
        RaycastHit hit;
        Debug.DrawRay(firstTouchRay.origin,direction3D ,Color.red,50.0f);

        //! check if first touch hits the coin
        if(Physics.Raycast(firstTouchRay.origin,firstTouchRay.direction,out hit))
        {
            Vector3 hitLocation = hit.point;
            if(hit.collider.tag == "Coin")
            {

                  coinRigidBody.AddForceAtPosition(direction3D *power, hitLocation );
                  throwCoinObject.throwWithForce(power,hitLocation,direction3D);
            } 

        }
    }
\$\endgroup\$
0
\$\begingroup\$

Worked when i changed

lastTouchPos = new Vector3(touch.position.x,touch.position.y,Camera.main.farClipPlane);

to

lastTouchPos = new Vector3(touch.position.x,touch.position.y,Camera.main.nearClipPlane);
\$\endgroup\$

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.