Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mischa.Silden · Jan 28, 2012 at 06:49 PM · character2.5dgetaxissidescroller

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);
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges