How do I add to a vector3 value? !!!Urgent!!!
I need to add a variable to a Vector3.up value, here is an example of what I want to do.
//working
if (followYAxis == true)
{
cam.transform.position.y = new Vector3 (0, playerController.transform.position.y, 0);
}
//not working
if (followYAxis == true)
{
cam.transform.position.y = new Vector3 (0, playerController.transform.position.y += camHeightOffset, 0);
}
Answer by Bunny83 · Jun 19, 2016 at 04:14 AM
Uhm, from your code it's not really clear what you actually want to do. Both examples you've given won't work and won't even compile in C#.
Apart from the fact that you can't assign a component(x, y or z) of the position property seperately due to the value type - property problem, you can't assign a Vector3 value to a float variable.
If you just want to add a certain amount to along the Vector3.up axis you can do:
cam.transform.position = playerController.transform.position + Vector3.up * camHeightOffset;
or
Vector3 temp = playerController.transform.position;
temp.y += camHeightOffset;
cam.transform.position = temp;
This will set the "cam" object at the same position as the player controller but offset by camHeightOffset.
Idk because Im not great at coding, but I would think the first one would work since it gives no errors in console.
First one wont work since this is inside of an Update function so it would end up multiplying it every frame, I tried it at already as well.
Nvm, the first one works, it was something else in the script giving me an error.
Answer by Zach28 · Jun 19, 2016 at 05:05 AM
You should use "cam.transform.position" not "cam.transform.position.y"
if (followYAxis == true)
{
cam.transform.position = new Vector3 (cam.transform.x , playerController.transform.position.y + camHeightOffset, cam.transform.z);
}
This doesn't work, gives me like 7 errors, I tried adding position after the transforms of x and z, but it still didn't work.