Raycast will be the direction of my bullet ?

I want to use my raycast to guide my bullet direction. my player camera will point to any spot in the game and when he press “F” the tower gun will fire at that spot. But i can’t calculate the raycast hit point so then use it for the bullet !
Here is raycast code:

    Camera mycamera;
    public float bulletGoalx,bulletGoaly,bulletGoalz;

    void Start() {
        mycamera = GetComponent<Camera>();
    }

    void Update() {
      
        Vector3 rayOrigin = new Vector3(0.5f, 0.5f, 0f); // center of the screen
        float rayLength = 500f;

        Ray ray = Camera.main.ViewportPointToRay(rayOrigin);
        Debug.DrawRay(ray.origin, ray.direction * rayLength, Color.red);

        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, rayLength))
        {
            print ("I'm looking at : " + hit.transform.name);
//            bulletGoalx =
//            bulletGoaly =
//            bulletGoalz =
        }

   
}

Bullet code:

    GameObject prefab;
    public GameObject angle;

    void Start () {
        prefab = Resources.Load("TowerBullet") as GameObject;

    }
  
    // Update is called once per frame
    void Update () {

    if (Input.GetMouseButtonDown(0))
    {
        GameObject bullets = Instantiate(prefab) as GameObject;
          bullets.transform.position = transform.position + angle.transform.forward * 0.1f;
        Rigidbody rb = bullets.GetComponent<Rigidbody>();
            rb.AddForce(angle.transform.forward * 50);
  
        Destroy(bullets, 9f);


    }

https://docs.unity3d.com/ScriptReference/RaycastHit-point.html

(ironically; all the right words, in the right order, just need less spaces and a dot :P)

2 Likes

if your camera move, this point is not the center of the camera.

ViewportPointToray is starting at the center of camera what you want is targeting forward the camera :

I did not got it. I want to calculate the distance X & Y so my bullet will move to this spot.

Thank you for mentioning this.
Yes, my camera is moving with the mouse or analogue direction and my code run fine. I can see that from the DrawRay.

Now you need to check if the ray hit something

if (Input.GetMouseButtonDown(0))
   {
Ray ray =Camera.main.ScreenPointToRay(Camera.main.transform.forward);
if (Physics.Raycast(ray, out hit, rayLength))
       {
        prefab = Resources.Load("TowerBullet") as GameObject;
       GameObject bullet = Instanciate(prefab) as GameObject;
      bullet.transfrom.position = [the position you want your bullet to start like the tower.transform];
bullet.GetComponent<BulletScript>().direction = hit.point;
       }
}
public class BulletScript
{
   public RigidBody rb;
   GameObject prefab;
   public GameObject angle;
   public Vector3 direction;
   void Start () {
  
rb = this.GetComponent<RigidBody>();
   }

   // Update is called once per frame
   void Update () {

   rb.MovePosition(direction * time.deltaTime);

}
}

hit.point is in world space so it is only the correct direction if you’re at the origin… if you want to use RaycastHit.point you’d need calculate the direction from something like “hit.point -transform.point” or, given that you’ve already calculated a ray you could just get it’s direction.

https://docs.unity3d.com/ScriptReference/Ray-direction.html

This work if the tower is at the same position as the camera

Thank you for you reply.
I tried to formate both scripts and i got one error in camera script and i could not fix it :rage:
This is camera script:

     Camera mycamera;
    public GameObject angle; // tower angle
    public Rigidbody rb;
    GameObject prefab;
    public Vector3 direction;

    void Start() {
        mycamera = GetComponent<Camera>();
        prefab = Resources.Load("bullets3") as GameObject;
    }

    void Update() {

        Vector3 rayOrigin = new Vector3(0.5f, 0.5f, 0f); // center of the screen
        float rayLength = 500f;
        RaycastHit hit;

//        Ray ray = Camera.main.ViewportPointToRay(rayOrigin);
//        Debug.DrawRay(ray.origin, ray.direction * rayLength, Color.red);
//
//        if (Physics.Raycast(ray, out hit, rayLength))
//        {
//            print ("I'm looking at : " + hit.transform.name);
//                        bulletGoalx =
//                        bulletGoaly =
//                        bulletGoalz =
//        }

        if (Input.GetMouseButtonDown(0))
        {
            Ray rays =Camera.main.ScreenPointToRay(Camera.main.transform.forward);
            if (Physics.Raycast(rays, out hit, rayLength))
            {
                GameObject bullets3 = Instantiate (prefab) as GameObject;
                bullets3.transfrom.position = transform.position + angle.transform.position; // it keep give me error here !!!!!!
                bullets3.GetComponent<Bscript>().direction = hit.point;
            }
        }
// error code :: Error CS1061: 'UnityEngine.GameObject' does not contain a definition for 'transfrom' and no extension method 'transfrom' accepting a first argument of type 'UnityEngine.GameObject' could be found.
    }

this is bullet script:

        public Rigidbody rb;
        public Vector3 direction;

        void Start () {
        rb = this.GetComponent<Rigidbody>();
        }

        void Update () {
        rb.MovePosition(direction * Time.deltaTime);
        }

    }

On the line 35 : bullet.transfrom should be bullet.transform

sorry for the mispell

1 Like

My mistake also. :frowning:
Now bullets not moving from the tower ! I added a rigidbody to the bullet in the camera script.
here is the code:

if (Input.GetMouseButtonDown(0))    {
            Ray rays =Camera.main.ScreenPointToRay(Camera.main.transform.forward);
            if (Physics.Raycast(rays, out hit, rayLength))
            {
                GameObject bullets3 = Instantiate (prefab) as GameObject;
                bullets3.transform.position =  transform.position + angle.transform.position * 0.1f;         
                bullets3.GetComponent<bbbbb>().direction = hit.point;
                Rigidbody rbb = bullets3.GetComponent<Rigidbody>();
                rbb.AddForce(angle.transform.forward * 500);

            }
        }