Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Raycast Direction help

Discussion in 'Physics' started by RatPackGaming, Jan 18, 2021.

  1. RatPackGaming

    RatPackGaming

    Joined:
    Dec 27, 2018
    Posts:
    42
    Hi all! Hope all are safe and well.
    So im developing a FPS and am currently working on some enemy AI, specifically the shooting aspect. The player and AI shoot using raycasting and the players works perfectly ( shooting from the middle of the screen forward, with slight randomization as you would see for accuracy in call of duty)
    Code (CSharp):
    1.            Vector3 accuracyInput = new Vector3(Input.mousePosition.x + Random.Range(-               weapon.weaponAccuracy, weapon.weaponAccuracy),
    2.                 Input.mousePosition.y + Random.Range(-weapon.weaponAccuracy, weapon.weaponAccuracy));
    3.  
    4.                 Ray ray = Camera.main.ScreenPointToRay(accuracyInput);
    Having a bit of trouble with the enemy, however. Im looking to recreate the same effect as the player. This is what I had but the it seems to be a bit above the "target" even if the accuracy is set to 0. Other than being over the head of the target it appears to be working correctly

    Code (CSharp):
    1. Vector3 aiAccuracyInput = new Vector3(target.position.x + Random.Range(-weapon.weaponAccuracy, weapon.weaponAccuracy),
    2.                 target.position.y + Random.Range(-weapon.weaponAccuracy, weapon.weaponAccuracy), target.position.z);
    3.                 Ray ray = new Ray(transform.position, aiAccuracyInput);

    any ideas would be appreciated, thank you!