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

Understanding transform.rotation

Discussion in 'Scripting' started by Natsnok, Jul 28, 2022.

  1. Natsnok

    Natsnok

    Joined:
    Apr 11, 2022
    Posts:
    4
    I didnt unterstand why this funktions giving back different values. I also didnt find an explanation in the documentation
    can someone explain to me why this:

    Code (CSharp):
    1.         pos += transform.rotation * velocity;
    2.         Debug.Log(pos);
    gives back another value than this:

    Code (CSharp):
    1.         pos.x += transform.rotation.x * velocity.x;
    2.         pos.y += transform.rotation.y * velocity.y;
    3.         pos.z += transform.rotation.z * velocity.z;
    4.         Debug.Log(pos);
    _______________

    Im looking for a way that in my 2D shooter the bullets get effect by the current movment of the player.
    i think i have a way by using the player.GetRelativePointVelocity(nullpoint);

    my problem is that im not able to add it to the bullet movement.

    Code (CSharp):
    1.         if (plMove == new Vector3(0f, 0f, 0f))
    2.         {
    3.             plMove = movment.GetPlayerRelativeMov();
    4.             plMove = plMove / 50;
    5.         }
    6.         Vector3 totalVelocity = velocity + plMove;
    7.         Vector3 pos = transform.position + plMove;  //this is the problem line
    8.         pos += transform.rotation * velocity;
    9.         transform.position = pos;
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,945
  3. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,689
    Because rotations are not vectors, but quaternions.

    And quaternion-vector multiplication is not defined the same way as vector-vector multiplication. When you multiply a quaternion and a vector together, you're rotating the vector. When you multiply a vector by another vector, you're scaling it (often non-uniformly).
     
    Natsnok likes this.
  4. Natsnok

    Natsnok

    Joined:
    Apr 11, 2022
    Posts:
    4
    Thank you now it is working well.
    Im now just adding the playermovement to the y coordinat of the velocity

    Code (CSharp):
    1.         Vector3 pos = transform.position;
    2.         if (velocity == new Vector3(0f, 0f, 0f))
    3.         {
    4.             plMove = movment.GetPlayerRelativeMov();
    5.             plMove = plMove / 50;
    6.             velocity = new Vector3(0f, 1f, 0f);
    7.             velocity.y = velocity.y * BulletSpeed.y;
    8.             velocity.y = velocity.y + Mathf.Abs(plMove.x) + Mathf.Abs(plMove.y) + Mathf.Abs(plMove.z);
    9.             Debug.Log("mov = " + plMove + "   Vel = " + velocity);
    10.         }
    11.         pos += transform.rotation * velocity;
    12.         transform.position = pos;