I was trying to come up with an easier and faster way to write my scripts by making a variable called playerPosition and I also have a variable called myTransform.
private Transform myTransform;
private Vector3 playerPosition;
public int playerSpeed = 5;
// Use this for initialization
void Start ()
{
myTransform = transform;
playerPosition = myTransform.position;
// Spawn point x y z
// Postion to be at -3, -3, -1
//myTransform.position = new Vector3(-3, -3, -1);
playerPosition = new Vector3(-3, 3, -1);
}
I’m trying to use the playerPosition to change what position my object is at but it doesn’t. Do I need to make an Update to myTransform.position = playerPosition in order to make it work?
Yes, that’s pretty much the gist of it. Just to be clear, after you do your playerPosition calculations, put “myTransform.position = playerPosition;” in the Update() (or referenced from the Update, or you can- you get the gist of it ;)). Also, try to put it in the later end of Update()- or you can even put it in LateUpdate()
Choices, right?
Also, is declaring playerPosition as myTranfsorm.position necessary as you just overwrite it at line 15 anyways in the Start()?
I’ve just had this problem last night in my test project, but the answer is pretty straightforward if you’re into programming:
Besides the primitives, when you create an object, you don’t store that object directly, you store a reference where the object is stored in the memory. So for example, when you create a new object named A, then say B = A, and say B.x = 2; then A.x will be 2, since they reference the same object.
Now in your code you first create a reference and store the position of your transform (playerPosition). Then you change this reference to a new Vector3. At this step, you didn’t change the position of the transform, you changed what playerPosition references. If you want to load that Vector3 into the transform position, you’ll have to change what transform.position references, like what MDragon said.