Search Unity

Need help understanding raycast

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

  1. Smitty3264

    Smitty3264

    Joined:
    Sep 12, 2020
    Posts:
    38
    I'v been going by multiple tutorials and scripts to come up with this:

    Code (CSharp):
    1. here is what I have so far
    2.  
    3. Code (CSharp):
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using System.Runtime;
    7. using System.Runtime.InteropServices;
    8. using UnityEngine;
    9. using UnityEngine.Events;
    10. public class aim : MonoBehaviour
    11. {
    12.  
    13.     public Rigidbody rb;
    14.     public int clickForce = 10;
    15.     public bool strikeBall;
    16.     public float target;
    17.     private RayCastHit;
    18.     void start()
    19.     {
    20.         rb = GetComponent<Rigidbody>();
    21.     }
    22.     void FixedUpdate()
    23.     {
    24.  
    25.         if (Physics.RayCast(out hit))
    26.         {
    27.             Target = RayCast; hitPoint;
    28.         }
    29.         if (strikeBall)
    30.          
    31.         {
    32.             rb.AddForce(target * clickForce);
    33.         }
    34.     }
    35. }
    The intent is to use raycast to set a target position on x and y then I press the ui button to send a pool ball towards set target but I'm not quite sure how to use raycast. Also I want to lock the cue ball on the zed axis I'm working I 2D.
     
  2. benthroop

    benthroop

    Joined:
    Jan 5, 2007
    Posts:
    263
    What you have here is not telling the Raycast where to aim. What you should do is figure out where you want the ray to start from, and what direction you want it to go.

    Are you thinking the user would click the mouse and then the code would find where it should be in the world? Once you define that we can figure out the right syntax.