Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to make projectiles launch toward the camera centre

Discussion in 'Scripting' started by farooqiasfandyar, Apr 19, 2021.

  1. farooqiasfandyar

    farooqiasfandyar

    Joined:
    Apr 19, 2021
    Posts:
    32
    Hi there,

    I'm new to unity so I've been wondering if anyone can help me find a way of shooting projectiles into the camera centre.

    Apparently, the script I wrote somewhat misbehaves. I used:

    Vector3 direction;
    and then a raycast hit to assign a direction vector,
    direction=hit.point;

    Then added the vector to the velocity of the rigidbody.

    What happens is that the projectiles keep going toward the right of my position and never shoot straight into the screen centre.

    Help is appreciated.
    Thanks
     
  2. chengwang2077

    chengwang2077

    Joined:
    Nov 23, 2019
    Posts:
    131
    If your camera is an orthographic camera, or a perspective camera and the near clipping plane is equal to 0, then the position of the camera is the position of the center of the screen
    var direction = cam.transform.position - target.transform.position;
     
  3. farooqiasfandyar

    farooqiasfandyar

    Joined:
    Apr 19, 2021
    Posts:
    32
    What would be my target? hit.point from raycasts?
     
  4. chengwang2077

    chengwang2077

    Joined:
    Nov 23, 2019
    Posts:
    131
    The position of the projectile