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

How do I use Player.position +1f?

Discussion in 'Scripting' started by NintendoMaster00, Feb 12, 2016.

  1. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    I was wondering how should make my vector 3 Have Player.position .1f? I need this to be the exact place as the player but to start 1f above the player

    Here is my code:
    Code (CSharp):
    1. PlayrPosition = new Vector3(player.position.x, player.position.y, 1f);
    but I am looking to somehow change that to be something like this
    Code (CSharp):
    1. PlayrPosition = new Vector3(player.position.x, player.position.y, player.position + 1f);
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    player.position.z +1f

    Position is a vector3, the z component is a float
     
    Kiwasi likes this.
  3. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    When i try that there is a strange glitch where it thinks that it is a vector 2 and says:

    error CS1061: Type `UnityEngine.Vector2' does not contain a definition for `z' and no extension method `z' of type `UnityEngine.Vector2' could be found (are you missing a using directive or an assembly reference?)
    the strange part is that it is a vector 3
    Code (CSharp):
    1.    
    2.  public Vector3 PlayrPosition;
    3.     void Start()
    4.     {
    5.         PlayrPosition = new Vector3(player.position.x, player.position.y, player.position.z + 1f);
    6.     }
     
  4. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    So, the "player" variable is of type Vector2. Change that one to Vector3.
     
    Mycroft likes this.
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    "player" can't be a vector2, else it would be complaining about trying to access the "non existant" position attribute long before trying to access an attribute of the position...

    along the right idea though. @NintendoMaster00 what is "player"? Transform.position is a vector3, but things like touches and Canvas Rects return a Vector2 from their "position" attribute.

    http://docs.unity3d.com/ScriptReference/30_search.html?q=position
     
  6. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    Thanks player is a rigidbody2d I changed it to a rigidbody and it works fine now :)