Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Drag to Aim, Release to shoot?

Discussion in 'Scripting' started by ltrout1999, Feb 11, 2016.

  1. ltrout1999

    ltrout1999

    Joined:
    Oct 11, 2015
    Posts:
    125
    I am creating a 'clone' of the game Soccer Stars and I want to have my mouse drag on the disc and when it releases, to add a force proportional to the distance the mouse traveled to the disc. Does anyone have some example code on how to do this? I'm a visual learner so just saying you need this or this doesn't help me too much.
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Noones going to give you scripts, but this MIGHT help:
    Vector3 force = new Vector3();

    Update
    if mousebutton is held down
    force = (mouse.position in world coords) - position of gameObject you want to launch
    if mousebutton is released and force != Vector3.zero
    apply force to gameObject

    You may need to set the force.y to 0.​
     
  3. ltrout1999

    ltrout1999

    Joined:
    Oct 11, 2015
    Posts:
    125
    That helps. I'm not asking for the entire script, I'm just not sure how to set it up so little examples like this help me.
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    I forgot to add the force = Vector3.Zero after applying force