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

Question Return a fixed direction

Discussion in 'Scripting' started by Smaex26, Dec 1, 2021.

  1. Smaex26

    Smaex26

    Joined:
    Dec 15, 2020
    Posts:
    5
    Hello everyone!

    I ran into a little problem regarding the direction between one object and my mouse position. I want to get the direction between those two and then add a force to another object in this direction. All of this is working fine but the problem is the direction is changing the further the mouse is away from the object. As a result the power of the jump is of course higher or lower regarding how far the mouse is away from the object. Is there a way to get the direction but apply a fixed force so every jump is equally powerful? I thought normalizing the Vector would do the trick but it seems not.

    Here is the code (reduced):

    Code (CSharp):
    1. mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    2.  
    3. jumpDirection = (mousePos - jumpPos.transform.position).normalized;
    4.  
    5. rb.AddForce(jumpDirection * jumpForce, ForceMode2D.Impulse);
    Thank you!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,963
    This is the answer. As long as the Vector is nonzero, it will become magnitude 1.0 after normalizing.

    If that is not working for you above, then perhaps you are changing
    jumpForce
    ? To prove it replace jumpForce with a constant annd do more testing.

    Beyond that, you must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. Smaex26

    Smaex26

    Joined:
    Dec 15, 2020
    Posts:
    5
    First of all: Thank you for this detailed answer!

    I already debuged the direction, that's where I discovered that the values are changing depending on the mouse distance.

    Also the code should work properly, it is only executing once when the mouse button gets up (the debug also just returns one time so the code is only executed once).

    The jump force is not changing, checked this with a hard coded float.

    When I debug the direction I noticed the following:

    if my mouse is just to the right of the object the vector is something like (0.3, 0.0). The further away my mouse gets the higher the number will get (0.8, 0.0). I don't know if I understand normalized correctly but this doesn't look like it's returing 1...

    It must have something to do with this. Every other aspect should work as intended...

    I also attached a GIF where you can see that the force is different depending on the mouse distance: GIF 02.12.2021 01-34-32.gif
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,963
    We know that can't be a normalized Vector... but if you print the original vector quantity BEFORE you normalize it, I bet you would find it had nonzero Z component and that you normalized it as a Vector3, THEN put it in a Vector2.

    So:

    take the delta
    set the Z to zero
    normalize
    use that
     
    Smaex26 likes this.
  5. Smaex26

    Smaex26

    Joined:
    Dec 15, 2020
    Posts:
    5
    Yes! That absolutely works and makes sense. Thanks again for the very detailed answers, that was really helpful!