Search Unity

Anyone know how to instantiate a prefab towards mouse location?

Discussion in 'Scripting' started by ImNew2Unity, Mar 23, 2013.

  1. ImNew2Unity

    ImNew2Unity

    Joined:
    Feb 2, 2013
    Posts:
    21
    I have a diablo/lol/dota style camera, i have a click to move system and im having a bit of trouble trying to make a script that when i press "1" it shoots a prefab towards my mouse position. Can anyone help me out
     
  2. joshimoo

    joshimoo

    Joined:
    Jun 23, 2011
    Posts:
    266
    You need to transform mouse position from screen space into world space.
    Have a look at these things in the documentation:
    Camera.ScreenToWorldPoint
    Input.mousePosition

    That will give you a target position then you need to calculate a vector from your current position to the target (direction Vector)
    Vector3

    Then you need to instantiate your object and apply a force or translation along the direction vector.
    Instantiate
    Transform.Translate
    Rigidbody.AddForce

    Get used to looking at the Documentation/Scripting Reference:
    http://docs.unity3d.com/Documentation/ScriptReference