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

help with making bullet move toward the mouse pointer

Discussion in 'Scripting' started by cal23, Apr 29, 2014.

  1. cal23

    cal23

    Joined:
    Apr 29, 2014
    Posts:
    14
    hello guys, i am new at unity and java scripting, i found different solutions for this around here about how to do this but i do not get it.
    Code (csharp):
    1.  
    2. if(Input.GetAxis("Fire_bullet") Time.time > rate_time)
    3.     {
    4.         rate_time = Time.time + rate;
    5.         Instantiate(bullet_pre, Vector3(transform.position.x, transform.position.y,0), Quaternion.identity);
    6.        
    7.        
    8.     }
    the bullet comes out from the parent,
    the on another script i have
    Code (csharp):
    1. function Update () {
    2.     bull.Translate(Vector3(speed * Time.deltaTime, 0, 0));
    3.     }
    for the speed

    how do i make the bullet come out towards the direction of the mouse pointer? thank you for your time
     
  2. xxxsur

    xxxsur

    Joined:
    Aug 6, 2013
    Posts:
    12
    Code (csharp):
    1. function Update () {
    2.     var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); // Construct a ray from the current mouse coordinates
    3.     bull.transform.lookAt(ray); //make the bullet look towards mousePosition on Update, and then translate with your original script
    4.     bull.Translate(Vector3(speed * Time.deltaTime, 0, 0));
    5. }
    try this
     
  3. cal23

    cal23

    Joined:
    Apr 29, 2014
    Posts:
    14
    No appropriate version of 'UnityEngine.Transform.LookAt' for the argument list '(UnityEngine.Ray)' was found.
    i get this error :/
     
  4. xxxsur

    xxxsur

    Joined:
    Aug 6, 2013
    Posts:
    12
  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I hate to be the stickler about this. And I know you say your new to it all. There is an entire reference to all of this:

    http://docs.unity3d.com/Documentation/ScriptReference

    In that you will find the reference to the Transform object:

    http://docs.unity3d.com/Documentation/ScriptReference/Transform.html

    In that transform, you will find a reference to the LookAt method:

    http://docs.unity3d.com/Documentation/ScriptReference/Transform.LookAt.html

    Notice.. that LookAt, is has some capitol letters.

    Look at does not accept a Ray in one of its overloaded methods. It accepts a Transform or a Vector3.

    This means that xxsur's code is actually incomplete. He gets the ray, but does nothing with it. (a ray is a point of origin and a direction, thats it)


    So below is the code (with added code) to convert the ray coming from the mouse to a position in space relative to the bullet.
    Code (csharp):
    1.  
    2.     function Update () {
    3.         // Construct a ray from the current mouse coordinates
    4.         var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    5.        
    6.         // capture the forward position of the bullet
    7.         var lookAt : Vector3 = bull.transform.position + bull.transform.forward * 10;
    8.        
    9.         // create a plane in front of the bullet
    10.         var plane : Plane = new Plane(bull.transform.forward, lookAt);
    11.        
    12.         // do a plane.Raycast from the mouse ray to the plane in front of the bullet
    13.         var dist : float;
    14.         if(plane.Raycast(ray, dist)){
    15.             // if it hits, "bend" the bullet lookAt point towards that point.
    16.             lookAt = Vector3.Lerp(lookAt, ray.GetPoint(dist), 5 * Time.deltaTime);
    17.         }
    18.        
    19.         // assign the bullet's facing.
    20.         bull.transform.LookAt(lookAt);
    21.         bull.Translate(Vector3(speed * Time.deltaTime, 0, 0));
    22.     }
    23.