Search Unity

Shooting a projectile from player towards mouse ( problem with camera )

Discussion in 'Scripting' started by Elgskred, May 26, 2015.

  1. Elgskred

    Elgskred

    Joined:
    Oct 13, 2014
    Posts:
    9
    Hello

    I have seen several post on the same topic ( shot towards mouse), but I could not find any where problems were related to the camera position, so here I am.

    Problem:
    When the player object is in the center of the world, the projectile is 5-10 degrees off target. But once i start moving around the difference increases up to 180 degrees.
    My "Main Camera" is attached to the player object, following it around. The camere is placed at [0, 10, -15] when the player object is at [0, 0, 0] with a 30 degree angle down towards the player object.

    This is the code that sends the projectile towards the mouse:
    Code (csharp):
    1.  
    2. public class Projectile : MonoBehaviour {
    3.  
    4.     public float speed;
    5.     public Vector3 mousePos;
    6.     private Rigidbody rb;
    7.  
    8.     private Plane zeroYPlane;
    9.  
    10.  
    11.     void Start(){
    12.         rb = GetComponent<Rigidbody> ();
    13.    
    14.         zeroYPlane = new Plane (Vector3.up, Vector3.zero);
    15.  
    16.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    17.         float _hitDistance;
    18.         zeroYPlane.Raycast (ray, out _hitDistance);
    19.         mousePos = ray.GetPoint (_hitDistance);
    20.         mousePos = new Vector3 (mousePos.x, 0, mousePos.z);
    21.         mousePos.Normalize ();
    22.         rb.velocity = mousePos * speed;
    23.  
    24.     }
    25. }
    26.  
    Any help will be greatly appreciated.

    Please note that my coding knowledge is rather limited, so please try to keep it simple.
     
  2. Elgskred

    Elgskred

    Joined:
    Oct 13, 2014
    Posts:
    9
    I've been fumbling around trying to figure out the problem related to moving the player object(and the main camera).
    I figured out the cause, but I have no idea on how to fix it.
    It appears that the "Camera.main.ScreenPointToRay(Input.mousePosition);" line does not update when I move the camera.
    In order to fix this, do I have to attack the actual "Main Camera" as a Camera object to the script?
     
  3. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Put it into the update function. The start function gets called once, update is called on every frame.
     
  4. Elgskred

    Elgskred

    Joined:
    Oct 13, 2014
    Posts:
    9
    This does not work either, the only result I get is that the projectiles always moves towards the mouse pointer. This did not solve the problem with the camera.
     
  5. Elgskred

    Elgskred

    Joined:
    Oct 13, 2014
    Posts:
    9
    Woop I fixed the main issue. In order to fix the issue with the projectile suddenly being 180 degrees of target I had to add this:
    Code (csharp):
    1.  
    2. mousePos = new Vector3 (mousePos.x - transform.position.x, 0, mousePos.z - transform.position.z);
    3.  
    The projectile is still slightly of target, by about 5 degrees at most, any ideas on how to fix that?