- Home /
how to climb a vine in 2D gameplay?
i have part of the script done but i don't know how to make the up arrow from jumping go to just upward movement.
Script:
function OnTriggerStay(hit : Collider) {
if(hit.tag == "Vinetrig" && Input.GetKeyDown(KeyCode.UpArrow))
{
PlatformerController.canControl = false; //Turns off normal controls
//What should i put here to make the player "walk" upwards on the y axis?
}
}
Answer by Peter G · Aug 25, 2010 at 10:46 PM
Depending on how complicated you want your movement to be, you could do something simple like:
function OnTriggerStay(hit : Collider) {
if(hit.tag == "Vinetrig" && Input.GetKeyDown(KeyCode.UpArrow))
{
PlatformerController.canControl = false; //Turns off normal controls
cachedCharacterController.Move(Vector3.up * climbSpeed * Time.deltaTime); //Link to the character controller
//If you use Input.GetAxis, you can have the player move up or down without writing twice as much code.
}
}
Maybe, for some reason you can't but if you setup a vertical input in the input manager, then you will be able to save writing an identical function for the down.
//Moves player up and down. function OnTriggerStay(hit : Collider) {
if(hit.tag == "Vinetrig" && Input.GetAxis("VerticalAxis")) //Is the user
//pressing up or down.
{
PlatformerController.canControl = false; //Turns off normal controls
cachedCharacterController.Move(Vector3.up * Input.GetAxisRaw("VerticalAxis") * climbSpeed * Time.deltaTime); //Link to the character controller
//Move based on the value of the input.
}
}
what should the "cachedCharacterController" variable be? (EX: var cachedCaracterController = )
There are 2 ways of doing it. The slightly longer way the in much better for performance or the quick to write slow version. 1st method: var cachedCharacterController : CharacterController; function Start () { cachedCharacterController = GetComponent(CharacterController); } The slow method. Take out the cachedCharacterController var and simply say GetComponent(CharacterController).$$anonymous$$ove(); in OnTriggerStay()
Your answer