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

Directions

Discussion in 'Scripting' started by Paykoman, Sep 20, 2016.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys, im prototyping a click to move top down "rpg" and i would like who can i achive my animations/particle effectes to be instantiated in my player but in mouse direction, so for example if im moving right and use a spell with mouse at left side of player it turns and attack in that direction. Ty for any help/info
     
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,738
    Get the position of your player, get he position of you click, subtract the 2 vectors and you got a direction and magnitude to the target.
     
  3. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    I dont want to click to attack, i only want click to move, i would like to use speel in numbers 1, 2 ,3 and so on..
     
  4. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Then replace "get position of click" with "get position of mouse".
     
  5. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    So and if i would like to attack in the mouse direction but distance independete, like a have a mouse 10 units but the spell need travel 5 ou 15 units, or even damage in a certain cone shape? i hv to put that information info in script attached to spell/prefab?
     
  6. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Your going to want to use:
    • Input.mousePosition -- this gets the mouse x,y position on screen
    • camera.WorldToScreenPoint(player.transform.position) this gets the x,y position of the player
    These are in screen position where 0,0 is bottom left, and ScreenWidth,ScreenHeight is top right

    So whoever has the smaller X is to the left and whoever has the smaller Y is on bottom relative to each other. Just compare their x co-ordinates to determine if you need to flip the player left/right and the Y part if you have any kind of way of pointng up/down