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

Question The length of the normalized vector changes with the distance of the cursor from the main character.

Discussion in 'Physics' started by bugrahanozcan16, Oct 30, 2022.

  1. bugrahanozcan16

    bugrahanozcan16

    Joined:
    Oct 30, 2022
    Posts:
    4
    I am making a 2D Shooter-Platform game and learning new stuff. But I don't understand why the normalized vector size keeps changing. I will use it for the direction of my rigid body velocity. If vector size changes, velocity changes too. I calculate normalized vectors with standard calculation, not with the integrated method.

    This is to calculate the direction vector.
    Code (CSharp):
    1. Vector3 cursorVector =  Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,-10f)) - new Vector3(transform.position.x,transform.position.y,0f);
    2.         aimDirection = cursorVector / cursorVector.magnitude;
    3.         aimVector = transform.position + aimDirection;
    And this is for velocity.
    Code (CSharp):
    1. instantiated.GetComponent<Rigidbody2D>().velocity = aimDirection * projectileSpeed;
     
  2. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    You are taking a Vector3 (cursorVector), converting it to a unit vector by scaling each dimension by the reciprical magnitude in all 3 dimensions (aimDirection) and then using just x and y for your Rigidbody 2D (the z component is discarded). You cannot take just the x and y components of a 3D unit vector and expect them to give you a 2D unit vector (unless z = 0, which in your case it does not).
     
    Last edited: Oct 31, 2022
  3. bugrahanozcan16

    bugrahanozcan16

    Joined:
    Oct 30, 2022
    Posts:
    4
    I changed and it works like a maximum direction vector. if I gave closer inputs to my character, the magnitude keeps changing.