Search Unity

TopDown shooting Above/Under Player

Discussion in 'Scripting' started by Wazanoob, Jun 13, 2021.

  1. Wazanoob

    Wazanoob

    Joined:
    Apr 18, 2021
    Posts:
    10
    Hello everyone, I'm doing a quick TopDown game to practice and learn scripting.

    I would like to make a map that isnt just FLAT, I want hills where the player can shoot from above...
    The thing is I can't find a code to do so, to change the aim in the Y axes, without making my player look at his feets.

    I tried to change a part of this code that I use to rotate the player to look at the mouse
    But after a lot of tries and fails... I come cry for help

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         Ray cameraRay = cam.ScreenPointToRay(Input.mousePosition);
    4.         Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
    5.         float rayLenght;
    6.  
    7.         if (groundPlane.Raycast(cameraRay, out rayLenght))
    8.         {
    9.             Vector3 pointToLook = cameraRay.GetPoint(rayLenght);
    10.             transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));
    11.         }
    12.     }
    Thanks for your help !
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    Unfortunately that's not specific enough for anyone to assist really.

    We know that .LookAt() works, so your task is to reason about why it isn't why it isn't working in this case.

    If I were you I would write some code to put the player at a known location (say (0,0,0)) and then make him look at (4,-2,4) and see if it looks correct. Remember LookAt() points the +Z vector in that direction.

    Beyond that, you could always keep the player vertical (only rotate around Y axis) and have the bullets go up / down appropriately by aiming them differently than the player's look.
     
  3. Wazanoob

    Wazanoob

    Joined:
    Apr 18, 2021
    Posts:
    10
    So I tried to rethink the problem, and I've down a little drawning to explain my problem and what I want hahaha
    Will probably be better than if I explain in english :rolleyes:

    The problem is I have no idea how to do a method for that


    sorry for being a noob and asking stupid questions :(
     

    Attached Files:

  4. Wazanoob

    Wazanoob

    Joined:
    Apr 18, 2021
    Posts:
    10
    OOoooooohhhh I found it !
    I'm stupid I used a method with " Plane groundPlane = new Plane(Vector3.up, Vector3.zero);"
    Nothing was working because of that.

    Here my new code very... very... simple and working as intended

    Code (CSharp):
    1. Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    2.         if (Physics.Raycast(ray, out RaycastHit raycastHit))
    3.         {
    4.             transform.LookAt(new Vector3(raycastHit.point.x, raycastHit.point.y + 1f, raycastHit.point.z));
    5.         }
    Took me an entire day to figure it out -_-
    I really need to find more course and read the line I'm finding on the web bruh.
     
    Kurt-Dekker likes this.