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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Character look and move in direction of the cursor. Top-down shooter.

Discussion in 'Scripting' started by Kirill_N, Jan 22, 2017.

  1. Kirill_N

    Kirill_N

    Joined:
    Jan 15, 2017
    Posts:
    2
    Hello, guys!
    I'll be very thankful for help in making top-down controller like in "Metrocide" game.
    I attached the link on the short video and hope anybody know how to make the same controller :)



    Please, help!
     
  2. Talthilas

    Talthilas

    Joined:
    Apr 1, 2014
    Posts:
    44
    Are you using a console controller or a mouse for this?
     
  3. Kirill_N

    Kirill_N

    Joined:
    Jan 15, 2017
    Posts:
    2
    Hi, I'm using mouse
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Kirill_N

    Hi there,

    this shouldn't be complicated at all;

    Your player moves in plane... you don't need to even raycast to floor,

    you could just have a imaginary plane.

    Unity already has a Plane class, that you can raycast against;

    Create a plane object... not plane geo, but in code (Plane groundPlane).

    Then you can raycast againts it:

    Code (csharp):
    1.  
    2. if (groundPlane.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
    3. {
    4. // do heading stuff
    5. }
    6.  
    You can decide your "heading" i.e. where you nose is pointing, by building a direction vector (pos B - pos A).

    Using this vector you, you can make your transform look at that direction... but you don't want to make it's forward this direction... as you would most likely point your nose up or down.

    I hope this made sense and I didn't make an error, it's bit too late here already... there might be other ways to do this, but this just came to my mind.