Search Unity

a little help with ui buttons

Discussion in 'Getting Started' started by Smitty3264, Oct 25, 2020.

  1. Smitty3264

    Smitty3264

    Joined:
    Sep 12, 2020
    Posts:
    38
    hello all. I managed to find a script that works for my billiards game to be used to hit the cue ball.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Events;
    5.  
    6. public class aim : MonoBehaviour
    7. {
    8.  
    9.     public Rigidbody rb;
    10.     public int clickForce = 10;
    11.     public bool strikeBall;
    12.  
    13.     void start()
    14.     {
    15.         rb = GetComponent<Rigidbody>();
    16.     }
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    21.         var mouseDir = mousePos - gameObject.transform.position;
    22.         mouseDir.z = 0.0f;
    23.         mouseDir = mouseDir.normalized;
    24.  
    25.         if (strikeBall = true)
    26.         {
    27.  
    28.             rb.AddForce(mouseDir * clickForce);
    29.         }
    30.     }
    31. }
    I added that bool and if statement and enable the bool in my ui button.
    intent was for the player to hit the ui button to send cue ball to last clicked position.
    instead the ball adds force to position of the mouse cursor without pressing the ui button
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    On line 25, you are assigning true to strikeBall. I know you think you're comparing the value to true, but you're not (that would use == rather than =).

    Also, there's never any need to say if (something == true). Just use if (something) instead.
     
  3. Smitty3264

    Smitty3264

    Joined:
    Sep 12, 2020
    Posts:
    38
    Thanks for the reply. I was looking up info on raycast and thought I could use it to set target if I addend a 2D box collided to my table. Then instead of adding force to mouse direction I could addforce to target. Problem is when I declare it at the top I get error 1585 on the next line.


    Guess I'm following a tutorial that doesn't work for me
     
  4. Smitty3264

    Smitty3264

    Joined:
    Sep 12, 2020
    Posts:
    38
    here is what I have so far

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Runtime;
    4. using System.Runtime.InteropServices;
    5. using UnityEngine;
    6. using UnityEngine.Events;
    7.  
    8. public class aim : MonoBehaviour
    9. {
    10.    
    11.     public Rigidbody rb;
    12.     public int clickForce = 10;
    13.     public bool strikeBall;
    14.     public float target;
    15.     private RayCastHit;
    16.  
    17.     void start()
    18.     {
    19.         rb = GetComponent<Rigidbody>();
    20.  
    21.     }
    22.  
    23.     void FixedUpdate()
    24.     {
    25.    
    26.         if (Physics.RayCast(out hit))
    27.         {
    28.             Target = RayCast; hitPoint;
    29.         }
    30.  
    31.         if (strikeBall)
    32.            
    33.         {
    34.  
    35.             rb.AddForce(target * clickForce);
    36.         }
    37.     }
    38. }