- Home /
a variable that allows you to choose either x,y, or z?
I have a script that makes the character move 1 space in the Z direction, when you press "f" by a door it will move you inside, but i want it to be a variable because then i can attach it to building with a different direction door. so basicly how do i make a variable that you either fill in a letter or has three options... each a letter?
Answer by burnumd · Jun 13, 2011 at 08:31 PM
Add a Vector3 public variable to the script. When you move the player, move it by that amount.
So, if right now you're doing something like
playerTransform.position.z += 1;
Instead do
var playerMoveAmount : Vector3;
function MyTrigger ()
{
playerTransform.position += playerMoveAmount;
}
I'm making a lot of assumptions because you didn't post any code, but you'd make sure MyTrigger is called whenever the event that causes the player to move happens.
Answer by Anxo · Jun 13, 2011 at 08:15 PM
I think your talking about a Vector3? which is a position in space and allows you to store X,Y,Z but the way you are using it sounds like you might be better off using 3 different variables.
what i mean is, and i have thought about your way, is i have something like Player.transform.position.z += 1;
but ins$$anonymous$$d of z i want to have a variable so i can change it without ruining the script on different doors, do you understand what i mean, i can do it like you said but that is more room for error with so many number, all the decimals and what not.
sorry, I am not quite sure what the problem is. Could you explain a little more .
Answer by Chrisg · Jun 13, 2011 at 11:43 PM
Do you mean you want the script to be robust, and able to be placed in a 3d world regardless of door rotation, so the character can "use" the door and be placed on the otherside regardless of if that's 1 in the z, 5 in the z, or 5 in the y?
var moveDist : int = 5;
function OnMouse()
{
//collision check - make sure there's something there, not empy space
var hit = physics.raycast(transform.position, vector3.forward*moveDist, hit);
//make sure it's the door, and not some kind of manatee
if(hit.tag = "Door")
//Translate("jump to") to a position that is forward along the current rotation of the //current object(ie, z-axis), by an amount equal to moveDist(vector3.forward is 0,0,1)
transform.Translate(vector3.forward * moveDist);
}
Might have syntax wrong, did it from my head, but a quick doc check will sort that out. moveDist will be avaliable via the editor, and you can change it like any other object property - if your doors are six units thick, set it to 6. Apply the script to the camera or the player object.