Common Conversions

Hello!
I’ve been enjoying the Unity 3D class so far. As the instructors have been delving deeper and deeper into coding, I realize that I am taking longer to formulate responses to the challenges given. I was doing some self-analysis for why I am taking longer than before, and I realized that it’s the issue with what I would like to call “conversions” .

For example, if I needed to convert a Vector3 into a position because a method needed it, I would take a long time until I realized transform.position can convert to a Vector3. Because I don’t understand at a fundamental level how I can “convert” a data or variable into other types of data or variable, it’s becoming increasingly difficult to access different areas of the hierarchy or a property of a variable.

I understand that because there are infinite ways to write coding, there isn’t really a set answer. But at the very least, what are commonly used “conversions” in Unity?

1 Like

First, transform.position doesn’t ‘convert’ into a Vector3, it is a Vector3.

I’m not 100% sure what you are asking but I’ll give it a stab. What I find often is:

  • Vector3Vector2Vector3
    You can cast it both ways. Casting Vector3 to Vector2 will lose the z
  • QuaternionVector3Quaternion
    You can use Quaternion.eulerAngles to get a Vector3 as it appears in the inspector’s rotation bits. The vector will hold the degrees for each axis.
    You can also get a Quaternion from Vector3 with Quaternion.Euler(Vector3). Note that converting it to Quaternion applies the axis rotations in a specific order (first z, then x, then y) and that is important because it can affect the final rotation. Although I’ve never had to worry about this, it’s important to know this in case you do run into issues
  • floatint
    I mention this one because there are 3 different methods in Unity and each one has it’s own use to consider. Actually, there are others but these are specific and can be the answer to you hitting an enemy with 1 health for 1 damage and it not dying.
    We have Mathf.RoundToInt(float) which will just round the decimal like we kinda learned in school. If the fraction is less than 0.5 it will round down, if it’s more than 0.5 it will round up and if it is exactly 0.5 it will round to the even integer. That is; 1.5 will become 2 and 2.5 will also become 2.
    Then there’s Mathf.FloorToInt(float) which will always round down (1.1 will become 1 and 1.9 will also become 1), and
    Mathf.CeilToInt(float) which will always round up (1.9 will become 2 and 1.1 will also become 2).
    These are easy to remember because the ‘floor’ is down and the ‘ceiling’ (‘Ceil’ is short for ceiling) is up.

That’s about it, though. I can’t think of any other conversions right now.

1 Like

This is exactly what I wanted. Thank you so much. I don’t plan on memorizing it, but having this “cheat sheet” of sorts will help me troubleshoot much faster. I appreciate the comments as well, especially on something like Mathf.RoundToInt(float) – I can definitely see myself scratching my head asking why it would round down when the program wanted to just round to the nearest even number.

I know you’ve replied to other questions that I had. Again, thank you for your response. I really do appreciate it!

Glad I could help

You don’t have to. Use it enough times and it will start to stick. I wrote all of this from memory. Except the order in which Quaternion.Euler(...) applies rotations. I had to look that up.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms