I have a platform that moves up and down, and i store this position in my custom class to designate type of behavior to do on such a position. Like this:
void Awake(){
foreach(Transform T in gameObject.transform){
if(T.name == "Floor"){
platform = T.gameObject;
enterPoint = new PathPoint("Wait",T.position);
break;
}
}
}
void Update(){
Debug.Log(enterPoint.position);
}
My hierarchy looks like this:
Platform¬
Floor
Now when i am moving the parent game object (Platform) the “Floor” object also moves, but enterPoint.position never changes. I was under the impression my PathPoint would have a reference to T.position not an assignment that never changes?
My PathPoint looks like this:
public class PathPoint {
public string type {get; private set;}
public Vector3 position { get; private set;}
public PathPoint (string t, Vector3 pos){
type = t;
position = pos;
}
}
So is there a way so that it will be referencing the position? I assumed it would do it by default?
The problem is normally it receives a Vector3 which is not a reference to a transform position, but rather a created Vector3 some where in the world, which would mean i am a bit lost how i would structure PathPoint to accept both ?
The reason now i need a transform position is because this new path point is on an object of which is also moving… which meant i needed a slightly different approach.
Without any context, it’s hard to understand what is your use cases and how it would be best to structure things.
you can add a Transform as another property in your class and allow to pass null, and return either the stored vector or the current vector of a transform based on what you need, and the status of the transform property. With your current code I’d do this:
public class PathPoint
{
public string type { get; private set; }
public Transform transform { get; private set; }
private Vector3 m_position;
public Vector3 position
{
get
{
if (transform != null)
return transform.position;
else
return m_position;
}
private set
{
m_position = value;
}
}
public PathPoint(string t, Vector3 pos)
{
type = t;
position = pos;
}
public PathPoint(string t, Transform trans)
{
type = t;
transform = trans;
}
}
Ah thank you i had tried some thing like this by creating new transforms but it seems they had to be attached to game objects in order to do that! This worked perfectly!