Search Unity

shooting in direction of mouse/crosshair

Discussion in 'Scripting' started by TheRaider, Aug 14, 2013.

  1. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    Code (csharp):
    1. var prefabBullet: Transform;
    2. var shootForce: float ;
    3.  
    4. function Update () {
    5.  
    6.    if(Input.GetButtonDown("Shoot")  )
    7.    {
    8.        var instancebullet =Instantiate( prefabBullet ,transform.position,Quaternion.identity);
    9.        instancebullet.rigidbody.AddForce(transform.forward * shootForce);
    10.    }
    11.    
    12. }
    So that is how you shoot straight from an object, how would I instead shoot in the direction of the mouse cursor on the screen?
     
  2. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You can first create a ray using Camera.main.ViewportPointToRay and then use its direction property.
     
  3. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    Code (csharp):
    1. #pragma strict
    2.  
    3. var mainCamera : Camera;
    4. var prefabBullet: Transform;
    5. var shootForce: float ;
    6.  
    7. function Update () {
    8.  
    9.    if(Input.GetKeyDown(KeyCode.RightShift))
    10.    {
    11.        var ray : Ray = mainCamera.ScreenPointToRay (Vector3(Input.mousePosition.x,Input.mousePosition.y,0));
    12.        
    13.        var instancebullet =Instantiate( prefabBullet ,transform.position,Quaternion.identity);
    14.        instancebullet.rigidbody.AddForce(ray.direction * shootForce);
    15.    }
    16.    
    17. }
    like that? it doesn't seem to quite behave right. The character is placed on the camera.

    It seems to work up and down. It also works to the right, but not to the left.
     
    Last edited: Aug 14, 2013
  4. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I think you're not making a classic FPS game where cursor is always centered. How about this solution: create your Ray just like you did and then do a raycasting. If it hits something, then your shooting direction should be "hitPosition - transform.position". If raycast doesn't hit anything, then use "Camera.main.ScreenToWorldPoint ( new Vector3 ( Input.mousePosition.x, Input.mousePosition.y, 1000 ) )" as the value of hitPosition.
     
  5. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    Yes I don't want the cursor always centred like a traditional FPS.

    I finally realised my issue. Because I was creating it inside the character controller it was getting messed up when I shot. So I just made a game object as a child in front of it and works fine now!
     
    Last edited: Aug 14, 2013
  6. freshBakedPie314

    freshBakedPie314

    Joined:
    Jan 20, 2020
    Posts:
    2
    You can create an gameobject at the center of the Screen as a child of the player and have lookAt script at the camera and move the projectile towards the gameobject