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

Directing at mouse pointer

Discussion in 'Scripting' started by tawdry, Feb 25, 2015.

  1. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Hi Guys

    Trying to get a projectile to head in the direction of mouse pointer but it just gives me my position.
    Not trying to hit anything specific just want it to head in the direction and angle the mouse pointer is situated at.
    Code (CSharp):
    1. if (Input.GetMouseButtonDown (0)) {// rather use a key than the actual mousedown here
    2.                     Vector3 targets;
    3.                 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition );
    4.                 targets = Camera.main.ScreenToWorldPoint (Input.mousePosition );// want targets to be the vector 3 the projectile heads to
    5.                 Debug.Log (targets);
     
    jalapen0 likes this.
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    I'm not entirely sure what you're asking. It looks like you've got the position (in the world) under the mouse pointer, so maybe your question boils down to: how to aim a projectile at a given world position?

    If that be the case, Quaternion.SetLookRotation is your friend. For example, something like this:
    Code (CSharp):
    1. transform.rotation.SetLookRotation(targets - transform.position, Vector3.up);
    HTH,
    - Joe
     
  3. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Ok solved it akinda
    added this line
    Vector3 mousePos=new Vector3(Input.mousePosition.x, Input.mousePosition.y, 11f);
    the z value(11f) not sure what effect it has as yet im using a self propolled particle so it s spped its effected by the z value far as i can tell . The higher value of z the more accurate it is for instance a z value of 1 will have the projectile almost shoot striaght up 11f is stightly upwards and 20f goes exactly where i pointed.

    Code (CSharp):
    1.     Vector3 mousePos=new Vector3(Input.mousePosition.x, Input.mousePosition.y, 11f);
    2.         if (Input.GetMouseButtonDown (0)) {
    3.                 Ray ray = Camera.main.ScreenPointToRay (mousePos );
    4.                 targets = Camera.main.ScreenToWorldPoint (mousePos );
    5.                 Debug.Log (targets);    //targets=wordPos;
    6.                             myScript.fireball ();
     
  4. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356

    Hey Joe here are the 2 parts to the script one is the aim and the other is execution I am interested in knowing how your line would change the scripts i'm using as in what parts would it substitute might be a better way to go for me performance wise?

    Code (CSharp):
    1. This is aiming script
    2.  
    3. [code=CSharp]    Vector3 mousePos=new Vector3(Input.mousePosition.x, Input.mousePosition.y, 11f);
    4.         if (Input.GetMouseButtonDown (0)) {
    5.                 Ray ray = Camera.main.ScreenPointToRay (mousePos );
    6.                 targets = Camera.main.ScreenToWorldPoint (mousePos );
    7.                 Debug.Log (targets);    //targets=wordPos;
    8.                             myScript.fireball ();

    And here is the execution script
    Code (CSharp):
    1. public    void fireball(){
    2.     //    target = myScript.targets;
    3.         if (myScript.tier==1){Debug.Log ("jghfjgh");
    4.             Vector3 direction =myScript.targets - transform.position;
    5.     Quaternion rotation = Quaternion.LookRotation (direction);
    6.         Instantiate (fire, transform.position, rotation);
    7.             damage=Random.Range (30,60);Debug.Break ();
    8.                     }
    [/code]
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Well, you're already using LookRotation. That's basically the same as what I was suggesting.