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

is it posible to convert a Vector3 to a float value?

Discussion in 'Scripting' started by benjaroman_84, Jan 12, 2022.

  1. benjaroman_84

    benjaroman_84

    Joined:
    Apr 26, 2021
    Posts:
    10
    Hello!

    Is there a way to conver a Vector3 to float value? in trying to identify the position of the center of the player (just X) and i need to convert a Vector3 to float to do that, is there a way for it?

    Thank you!
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    A Vector3 is 3 floats

    x, y, z

    If you want any of those specific values, just access that property.

    Otherwise... if you want some other 'float"? What would it be? The magnitude/length? Well access that magnitude property (it's just the sqrt of the sum of the squares of x,y,z).

    But it sounds like you want the x position:
    Code (csharp):
    1. float xpos = transform.position.x;
    (swap out 'transform' for whatever transform that you want the position of... this specifically gives you the position of the GameObject the script is attached to)
     
    Bunny83 likes this.
  3. benjaroman_84

    benjaroman_84

    Joined:
    Apr 26, 2021
    Posts:
    10
    thank you very much!
     
  4. benjaroman_84

    benjaroman_84

    Joined:
    Apr 26, 2021
    Posts:
    10

    tried that but dindt work :(
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Show your code
     
  6. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
  7. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    Be aware that if you map a Vector3 to one float, there will be a lost of information. That is, if you have a function that does this mapping (could be the magnitude, some dot product, ...), you will be able to know the float value for a given Vector3 but the reverse is impossible: you will not be able to retrieve the vector 3 from a given float. Also, it's possible that different Vector3s map to the same float. For instance, all unit vectors, by definition, have a magnitude of one.

    Could you tell us a bit more about what you want to do?