Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Throw Ball Script Not Working Properly

Discussion in 'Physics' started by ashperez8, Nov 22, 2023.

  1. ashperez8

    ashperez8

    Joined:
    Nov 22, 2023
    Posts:
    2
    Hello!

    I copied a ball throwing script I found from a tutorial and input it into my project. The script worked perfectly well in the tutorial, but something about it is incorrect in my project. I only made minor adjustments to the clipping plane. When I swipe my mouse in my project, the ball goes back to its starting position instead of going to the target. I'm not sure what else to adjust. Please help!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BallThrower : MonoBehaviour
    6. {
    7.     private GameObject Ball;
    8.  
    9.     float startTime, endTime, swipeDistance, swipeTime;
    10.     private Vector2 startPos;
    11.     private Vector2 endPos;
    12.  
    13.     public float MinSwipDist = 0;
    14.     private float BallVelocity = 0;
    15.     private float BallSpeed = 0;
    16.     public float MaxBallSpeed = 350;
    17.     private Vector3 angle;
    18.  
    19.     private bool thrown, holding;
    20.     private Vector3 newPosition;
    21.     Rigidbody rb;
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.         setupBall();
    27.     }
    28.  
    29.     void setupBall()
    30.     {
    31.         GameObject _ball = GameObject.FindGameObjectWithTag("Player");
    32.         Ball = _ball;
    33.         rb = Ball.GetComponent<Rigidbody>();
    34.         ResetBall();
    35.     }
    36.  
    37.     void ResetBall()
    38.     {
    39.         angle = Vector3.zero;
    40.         endPos = Vector2.zero;
    41.         startPos = Vector2.zero;
    42.         BallSpeed = 0;
    43.         startTime = 0;
    44.         endTime = 0;
    45.         swipeDistance = 0;
    46.         swipeTime = 0;
    47.         thrown = holding = false;
    48.         rb.velocity = Vector3.zero;
    49.         rb.useGravity = false;
    50.         Ball.transform.position = transform.position;
    51.     }
    52.  
    53.     void PickupBall()
    54.     {
    55.         Vector3 mousePos = Input.mousePosition;
    56.         mousePos.z = Camera.main.nearClipPlane * 5f;
    57.         newPosition = Camera.main.ScreenToWorldPoint(mousePos);
    58.         Ball.transform.localPosition = Vector3.Lerp(Ball.transform.localPosition, newPosition, 80f * Time.deltaTime);
    59.     }
    60.  
    61.     private void Update()
    62.     {
    63.         if (holding)
    64.             PickupBall();
    65.  
    66.         if (thrown)
    67.             return;
    68.  
    69.         if (Input.GetMouseButtonDown(0))
    70.         {
    71.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    72.             RaycastHit _hit;
    73.  
    74.             if (Physics.Raycast(ray, out _hit, 100f))
    75.             {
    76.                 if (_hit.transform == Ball.transform)
    77.                 {
    78.                     startTime = Time.time;
    79.                     startPos = Input.mousePosition;
    80.                     holding = true;
    81.                 }
    82.             }
    83.         }
    84.         else if (Input.GetMouseButtonUp(0))
    85.         {
    86.             endTime = Time.time;
    87.             endPos = Input.mousePosition;
    88.             swipeDistance = (endPos - startPos).magnitude;
    89.             swipeTime = endTime - startTime;
    90.  
    91.             if (swipeTime < 0.5f && swipeDistance > 30f)
    92.             {
    93.                 //throw ball
    94.                 CalSpeed();
    95.                 CalAngle();
    96.                 rb.AddForce(new Vector3((angle.x * BallSpeed), (angle.y * BallSpeed / 3), (angle.z * BallSpeed) * 2));
    97.                 rb.useGravity = true;
    98.                 holding = false;
    99.                 thrown = true;
    100.                 Invoke("ResetBall", 4f);
    101.             }
    102.             else
    103.                 ResetBall();
    104.         }
    105.     }
    106.  
    107.     private void CalAngle()
    108.     {
    109.         angle = Camera.main.ScreenToWorldPoint(new Vector3(endPos.x, endPos.y + 50f, (Camera.main.nearClipPlane + 5)));
    110.     }
    111.  
    112.     void CalSpeed()
    113.     {
    114.         if (swipeTime > 0)
    115.             BallVelocity = swipeDistance / (swipeDistance - swipeTime);
    116.  
    117.         BallSpeed = BallVelocity * 40;
    118.  
    119.         if (BallSpeed <= MaxBallSpeed)
    120.         {
    121.             BallSpeed = MaxBallSpeed;
    122.         }
    123.         swipeTime = 0;
    124.     }
    125. }
    126.