- Home /
Move FPS up/down one level in a building
Hi there,
I am actually using Player Relative Control Prefab from the Standard Assets (Mobile) Folder and i have a 3D model that have a few storey. The problem here is i am trying to move the character (Player Relative Control) Up/Down one storey upon clicking on the GUI Button of 'Up' or 'Down' respectively. But i would need to click on the 'Down' a few time before the character will actually move down by one storey. I am using the following Code:
character.transform.Translate(new Vector3(newPositionX, newPositionY, newPositionZ) * Time.deltaTime);
Someone can kindly advice me on what i should do to resolve this?
Thanks in advance!
Below is a sample of my problem code which should fire of when i click on the Button:
if (GUI.Button(new Rect(10, 60, 80, 30), "Down")) {
//AutoFade.LoadLevel(null, 0, 1, Color.black);
Debug.Log("Before current position: " + character.transform.position.y);
for (int i = 0; i < floorHeightArrayList.Count; i++) {
if ((character.transform.position.y - 1) <= float.Parse(floorHeightArrayList[i].ToString())) {
/*if ((character.transform.position.y - 1) == float.Parse(floorHeightArrayList[i].ToString())) {
newPositionX = character.transform.position.x;
newPositionY = float.Parse(floorHeightArrayList[i + 2].ToString()) + 1.0f;
newPositionZ = character.transform.position.z;
character.transform.position = new Vector3(newPositionX, newPositionY, newPositionZ);
break;
}*/
newPositionX = character.transform.position.x;
newPositionY = float.Parse(floorHeightArrayList[i - 1].ToString()) + 1.0f;
newPositionZ = character.transform.position.z;
//Debug.Log("newPositionY: " + newPositionY);
character.transform.Translate(newPositionX, newPositionY, newPositionZ, Space.Self);
//character.transform.position = new Vector3(newPositionX, newPositionY, newPositionZ);
break;
}
}
Debug.Log("After current position: " + character.transform.position.y);
}
Answer by WillTAtl · Nov 02, 2011 at 03:38 AM
if you want to instantly jump one floor when clicking the button, there's no reason to multiply by deltaTime. You do that when you want to move at some steady rate over time, as in an update() function, and you multiply deltaTime by the velocity, not by a target position.
Assuming newPositionX/Y/Z are the point you want to go to one floor up/down, just remove the "*Time.deltaTime" bit entirely.
hi WillTati,
Thanks for the reply. I want the character to like slowly move up/down one level. Currently i am trying on the following codes:
character.transform.Translate(newPositionX, newPositionY, newPositionZ, Space.World);
But it don't seem to have the 'feel' of the character is move up/down.
ah, sorry for the misunderstanding. I'm afraid I don't understand what's happening or what you're asking, then. Where are you using this line of code? In what function, and on what conditions?
It might help if you could add the entire function containing this line to the original question, ins$$anonymous$$d of giving just this one line!
Hi WillTatI,
Sorry for my poor explaination. I have attached above for the code that is suppose to fire off in a GUI Button.
it sounds like you want an elevator, but without the elevator? Search UA for questions about elevators, they should be helpful, you'll just be applying the behavior to the player directly ins$$anonymous$$d of to an elevator object!
Your answer
![](https://koobas.hobune.stream/wayback/20220613030504im_/https://answers.unity.com/themes/thub/images/avi.jpg)