c# - how can i get the location of the player along a specific axis during play mode? -
my player runs forward during play time want store current position along specific axis lets z in variable inside update function , use position elsewhere. want know kind of datatype have use store such value current position of player on z axis if doing possible. saw code somewhere:-
var playerpos:vector3 = playerobject.transform.position;
but idk how working if in first place
you close current syntax in code javascript.
you use transform.position
position of axis. transform.position
returns vector3
, vector3
contains axis gameobject position.vector3
has x
, y
, z
properties float
datatype.
accessing position:
assuming playerobject
name of gameobject, below example of how access each individual axis.
to x axis
float x = playerobject.transform.position.x;
to y axis
float y = playerobject.transform.position.y;
to z axis
float z = playerobject.transform.position.z;
you can position once , store vector3
variable access each individual axis there:
to axis (x,y,z)
vector3 playerpos = playerobject.transform.position; float x = playerpos.x; float y = playerpos.y; float z = playerpos.z;
modifying position:
modifying position different accessing it. can access this
float x = playerobject.transform.position.x;
but cannot change x
this:
playerobject.transform.position.x = 6f;
you must create new vector3
, modify axis want (x
, y
, z
), change transform.position
variable new variable(vector3
).
it should this:
vector3 newpos = playerobject.transform.position; newpos.x = 5f; newpos.y = 4f; newpos.z = 3f; playerobject.transform.position = newpos;
my suggestion use documentation. shows each property or method returns. prevent asking such question this. example, transform.position returns vector3
.
when click on vector3
, see many other variables can access it.
Comments
Post a Comment