I am trying to get the transform.position.x and the transform.position.z of an object, add an amount to it and then crate a new Vector3 with the new x,z coords without affecting the y. Basically so I can instantiate an object just behind it, kind of like this…
var xPos : float = transform.position.x + (size*pieces);
But I get this error:
Operator ‘+’ cannot be used with a left hand side of type ‘float’ and a right hand side of type ‘UnityEngine.Vector3’.
I do understand the error but I have been going around in circles trying to find a solution and I can’t. It really doesn’t seem like it should be so difficult.
If pieces is a vector, you can do something like this:
var temp: Vector3 = size * pieces; // calculate the vector offset
temp.y = 0; // ensure y will not be modified
var newPos: Vector3 = transform.position + temp; // calculate the new position
or like this:
var newPos: Vector3 = transform.position;
newPos.x += size * pieces.x; // add offset x
newPos.z += size * pieces.z; // add offset z