- Home /
Input.GetAxis to act like button
Hi! I've been trying to solve this all day but I just cant. My game is a 2.5 running game where the character automatically moves in +x axis. I want to have 3 separate lines to run along. The idea is that the character would change its running line when the right analog stick is tilted up or down and stick to that line until right analog stick is tilted again, up or down of course.
Here is my code so far:
using UnityEngine;
using System.Collections;
public class scr_PlayerTest : MonoBehaviour
{
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
public float dashDistance = 0;
private int jumpCount = 0;
private int laneNumber = 0;
private Vector2 abilityDash = Vector2.right;
private Vector2 moveDirection = Vector2.zero;
void Awake()
{
laneNumber = 0;
}
void Update()
{
CharacterController player = GetComponent<CharacterController>();
if (player.isGrounded)
{
// resets jump counter
jumpCount = 0;
// if player is on the ground, we normally don't want to move him vertically
moveDirection.y = 0;
// but if the jump button is pressed, then we do want him moving vertically
if (Input.GetButton("Jump"))
{
// first jump counter
jumpCount++;
moveDirection.y = jumpSpeed;
}
}
else
{
// if player is not on the ground, then apply gravity to him
moveDirection.y -= gravity * Time.deltaTime;
// allows small jumps
if (Input.GetButtonUp("Jump") && (jumpCount > 0))
{
// second jump counter
jumpCount++;
moveDirection.y = gravity * Time.deltaTime;
}
}
// I'll deal with this later
if (Input.GetButton("Dash"))
{
player.Move(abilityDash * 1);
print("we are dashing");
}
{ //HERE IT ALL GOES TO HELL!
float lanePos0 = Input.GetAxis("Change Lane") * transform.localPosition.z -7.5f;
float lanePos1 = Input.GetAxis("Change Lane") * transform.localPosition.z -2.5f;
float lanePos2 = Input.GetAxis("Change Lane") * transform.localPosition.z +2.5f;
if (laneNumber == 0)
{
transform.localPosition = new Vector3(0f, 0f, lanePos2);
laneNumber++;
}
if (laneNumber == 1)
{
transform.localPosition = new Vector3(0f, 0f, lanePos2);
laneNumber++;
}
if (laneNumber == 2)
{
transform.localPosition = new Vector3(0f, 0f, lanePos1);
laneNumber--;
}
}
// constantly move horizontally
moveDirection.x = speed;
// finally, we actually apply the movement to the player
player.Move(moveDirection * Time.deltaTime);
}
}
Answer by GS-Kiwibird · Sep 07, 2012 at 03:53 PM
You can use Input.GetAxisRaw("Change Lane") which will do the trick for the keyboard.
For the analog stick set the "Gravity" and "Sensitivity" to 100 in the Input Manager.
Your answer
Follow this Question
Related Questions
Keeping forward transform 2 Answers
Knockout Or Suicide Detection 1 Answer
how to fix the z position of an object 2 Answers
Flipping Sprite based in controller input 0 Answers
advice on how to implement 2.5D 1 Answer