Search Unity

a couple things wrong with this script

Discussion in 'Getting Started' started by Smitty3264, Nov 5, 2020.

  1. Smitty3264

    Smitty3264

    Joined:
    Sep 12, 2020
    Posts:
    38
    I am trying to set a target position as mouse position with the on mouse button down then hit a cue ball in that direction with a UI button.

    Code (CSharp):
    1. public class aim : MonoBehaviour
    2. {
    3.     [SerializeField] Transform target;
    4.     public Rigidbody rb;
    5.     public bool strikeBall;
    6.     Vector3 targetPos;
    7.  
    8.  
    9.  
    10.     void start()
    11.     {
    12.         targetPos = transform.position;
    13.         rb = GetComponent<Rigidbody>();
    14.         strikeBall = false;
    15.     }
    16.  
    17.     void FixedUpdate()
    18.     {
    19.  
    20.         if (Input.GetMouseButtonDown(0))
    21.         {
    22.             targetPos = (Vector3) Camera.main.ScreenToWorldPoint(Input.mousePosition);
    23.            
    24.         }
    25.  
    26.         if (strikeBall = !strikeBall) ;
    27.         {
    28.  
    29.             rb.AddForce(targetPos, ForceMode.Impulse);
    30.         }
    31.     }
    32. }
    so far the ball likes to target the y axis but x axis seems fixed. I chose my aim script and enable bool from the function list under the UI button but in game the ball targets the button itself on click. I have tried to input.mousePosition.x and y but I got errors. Probably I need to know how to declare input mouse position it up top. if someone tell me how to fix these problems I will appreciate it.