Search Unity

Question How can I throw a object to an location where the camera is aimed at?

Discussion in 'AR' started by realmcrafter, Dec 7, 2020.

  1. realmcrafter

    realmcrafter

    Joined:
    Nov 12, 2020
    Posts:
    7
    I'm creating a AR game where the player can throw an object. The problem is that the object can only be thrown at the direction of where the camera is aimed at the start of the game. So after loading the object and throwing it, it doesn't matter where I stand with my device. Even when I stand on the opposite with my device, the ball will instead going forward be aimed at me while swiping my finger forward.

    How can I make sure the ball will be throwed to where my camera is aiming at?

    code for swiping the ball

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SwipeScript : MonoBehaviour {
    6.  
    7.     Vector2 startPos, endPos, direction; // touch start position, touch end position, swipe direction
    8.     float touchTimeStart, touchTimeFinish, timeInterval; // to calculate swipe time to sontrol throw force in Z direction
    9.  
    10.     [SerializeField]
    11.     float throwForceInXandY = 1f; // to control throw force in X and Y directions
    12.  
    13.     [SerializeField]
    14.     float throwForceInZ = 50f; // to control throw force in Z direction
    15.  
    16.     Rigidbody rb;
    17.  
    18.     void Start()
    19.     {
    20.         rb = GetComponent<Rigidbody> ();
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update () {
    25.  
    26.         // if you touch the screen
    27.         if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
    28.  
    29.             // getting touch position and marking time when you touch the screen
    30.             touchTimeStart = Time.time;
    31.             startPos = Input.GetTouch (0).position;
    32.         }
    33.  
    34.         // if you release your finger
    35.         if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended) {
    36.  
    37.             // marking time when you release it
    38.             touchTimeFinish = Time.time;
    39.  
    40.             // calculate swipe time interval
    41.             timeInterval = touchTimeFinish - touchTimeStart;
    42.  
    43.             // getting release finger position
    44.             endPos = Input.GetTouch (0).position;
    45.  
    46.             // calculating swipe direction in 2D space
    47.             direction = startPos - endPos;
    48.  
    49.             // add force to balls rigidbody in 3D space depending on swipe time, direction and throw forces
    50.             rb.isKinematic = false;
    51.             rb.AddForce (- direction.x * throwForceInXandY, - direction.y * throwForceInXandY, throwForceInZ / timeInterval);
    52.  
    53.             // Destroy ball in 4 seconds
    54.             Destroy (gameObject, 3f);
    55.  
    56.         }
    57.            
    58.     }
    59. }