Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Dark Age · Jan 04, 2016 at 12:41 PM · movementgetaxisvertical

3 lane change scripting with vertical movement problem

I have a script I am working on and I believe I have the premise down but I am getting confused as to how to keep my 3 lane switching code and add in the Input.getaxis ("Vertical") to work so the character can move forward, or should I just write a script sepearate making my player always move forward (this is for an infinite runner style game)

the code is as follows: using UnityEngine; using System.Collections;

 public class ExampleClass : MonoBehaviour {
     public float speed = 6.0F;
     public float jumpSpeed = 8.0F;
     public float gravity = 20.0F;
     private Vector3 moveDirection = Vector3.zero;
 
     public int laneNumber = 0;
     public int lanesCount = 4;
     bool didChangeLastFrame = false;
     public float laneDistance = 2;
     public float firstLaneXPos = 0;
     public float deadZone = 0.1f;
     public float sideSpeed = 5;
 
 
     void Update() {
         CharacterController controller = GetComponent<CharacterController>();
         if (controller.isGrounded) {
             float input = Input.GetAxis("Horizontal");
             if(Mathf.Abs(Input) > deadZone) {
                 if(!didChangeLastFrame) {
                     didChangeLastFrame = true; //Prevent overshooting lanes
                     laneNumber += Mathf.RoundToInt(Mathf.Sign(input));
                     if(laneNumber < 0) laneNumber = 0;
                     else if(laneNumber >= lanesCount) laneNumber = lanesCount - 1;
                 }
             } else {
                 didChangeLastFrame = false; 
 
             moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
             if (Input.GetButton("Jump"))
                 moveDirection.y = jumpSpeed;
             
         }
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
     }
 }
 }
Comment
Add comment · Show 8
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
avatar image Adam-Halley-Prinable · Jan 04, 2016 at 01:10 PM 0
Share

take this code from update: CharacterController controller = GetComponent<CharacterController>();

and put it in a start function ins$$anonymous$$d, like this:

 void Start(){
 CharacterController controller = GetComponent<CharacterController>();
 }
avatar image Dark Age Adam-Halley-Prinable · Jan 04, 2016 at 01:49 PM 0
Share

I moved the code as you suggested into a a start function, but I am now getting this error

error CS1525: Unexpected symbol `}' and also a parsing error, but my code is all closed in ??, or have I missed one somewhere

 using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : $$anonymous$$onoBehaviour 
 {
     public float speed = 6.0F;
     public float jumpSpeed = 8.0F;
     public float gravity = 20.0F;
     private Vector3 moveDirection = Vector3.zero;
     
     public int laneNumber = 0;
     public int lanesCount = 4;
     bool didChangeLastFrame = false;
     public float laneDistance = 2;
     public float firstLaneXPos = 0;
     public float deadZone = 0.1f;
     public float sideSpeed = 5;
     
     
     void Start()
     {
         CharacterController controller = GetComponent<CharacterController>();
             }
     
     void Update() {
         
         float input = Input.GetAxis("Horizontal");
         if($$anonymous$$athf.Abs(Input) > deadZone) {
             if(!didChangeLastFrame) {
                 didChangeLastFrame = true; //Prevent overshooting lanes
                 laneNumber += $$anonymous$$athf.RoundToInt($$anonymous$$athf.Sign(input));
                 if(laneNumber < 0) laneNumber = 0;
                 else if(laneNumber >= lanesCount) laneNumber = lanesCount - 1;
             }
         }
         else 
         {
             didChangeLastFrame = false; 
             
             moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
             if (Input.GetButton("Jump"))
                 moveDirection.y = jumpSpeed;
             
         }
         moveDirection.y -= gravity * Time.deltaTime;        
         controller.$$anonymous$$ove (moveDirection * Time.deltaTime)
     }
 }
avatar image Adam-Halley-Prinable Dark Age · Jan 04, 2016 at 01:51 PM 0
Share

Semicolon on the last line

Show more comments
avatar image Adam-Halley-Prinable · Jan 04, 2016 at 01:17 PM 0
Share

What happens when you run this code as is?

avatar image Dark Age Adam-Halley-Prinable · Jan 04, 2016 at 02:06 PM 0
Share

ok I can get the player to move now but not in the 3 lane Horizontal anymore, he can move back and forwards now so that's a good start. when I hit the jump key he hangs in the air and slowly comes down, if I hit the jump key again he goes even higher, when I press the left/D key the character lowers back to the ground. code I have so far is as follows. I am out for the night its past midnight inthe land down under thanks for helping me out so far, will check in the morning, Thanks again. using UnityEngine; using System.Collections;

 public class ThreeLaneRunner : $$anonymous$$onoBehaviour 
 {
     public float speed = 6.0F;
     public float jumpSpeed = 8.0F;
     public float gravity = 20.0F;
     private Vector3 moveDirection = Vector3.zero;
     
     public int laneNumber = 0;
     public int lanesCount = 4;
     bool didChangeLastFrame = false;
     public float laneDistance = 2;
     public float firstLaneXPos = 0;
     public float deadZone = 0.1f;
     public float sideSpeed = 5;
     
     
     void Start()
     {
 
     }
     
     void Update() {
 
         CharacterController controller = GetComponent<CharacterController>();
         float input = Input.GetAxis("Horizontal");
         if($$anonymous$$athf.Abs(input) > deadZone) {
             if(!didChangeLastFrame) {
                 didChangeLastFrame = true; //Prevent overshooting lanes
                 laneNumber += $$anonymous$$athf.RoundToInt($$anonymous$$athf.Sign(input));
                 if(laneNumber < 0) laneNumber = 0;
                 else if(laneNumber >= lanesCount) laneNumber = lanesCount - 1;
             }
         }
         else 
         {
             didChangeLastFrame = false; 
             
             moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
             if (Input.GetButton("Jump"))
                 moveDirection.y = jumpSpeed;
             
         }
         moveDirection.y -= gravity * Time.deltaTime;        
         controller.$$anonymous$$ove (moveDirection * Time.deltaTime);
     }
 }
avatar image Dark Age Dark Age · Jan 04, 2016 at 11:50 PM 0
Share

ok so I noticed the Upper case on the input and changed t the same time you did, I have tried the code in the void Start and update and get the same problem both times, I can move forward and backwards, space for jump (endless Y movement) if I keep the button depressed. The right key "d" now works as a -y movement ins$$anonymous$$d of right and I have no left movement at all.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Dark Age · Jan 05, 2016 at 01:17 AM

Ok I have been able to work out the code for myself to get a 90% desired result, I can now change in only 3 lanes, jump, and I have also attached a script for zmovement. The last 10% is stopping the backwards movement. So for those wishing to do the same style of game/scripting I share with you what I have been able to achieve.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControllerScript: MonoBehaviour 
 {
     public float speed = 6.0F;
     public float jumpSpeed = 8.0F;
     public float gravity = 20.0F;
     private Vector3 moveDirection = Vector3.zero;
     
     public int laneNumber = 0;
     public int lanesCount = 4;
     bool didChangeLastFrame = false;
     public float laneDistance = 2;
     public float firstLaneXPos = 0;
     public float deadZone = 0.1f;
     public float sideSpeed = 5;
 
     
     void Update() {
         //Access the objects Character Controller
         CharacterController controller = GetComponent<CharacterController>();
 
         float input = Input.GetAxis("Horizontal");
         //Make sure the object is grounded before doing the math for lane changes
         if (controller.isGrounded)
         if(Mathf.Abs(input) > deadZone) {
             if(!didChangeLastFrame) {
                 didChangeLastFrame = true; //Prevent overshooting lanes
                 laneNumber += Mathf.RoundToInt(Mathf.Sign(input));
                 if(laneNumber < 0) laneNumber = 0;
                 else if(laneNumber >= lanesCount) laneNumber = lanesCount - 1;
             }
         }
         else 
         {
             didChangeLastFrame = false; 
             
             moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
             if (Input.GetButton("Jump"))
                 moveDirection.y = jumpSpeed;
             
         }
         Vector3 pos = transform.position;
         pos.x = Mathf.Lerp(pos.x, firstLaneXPos + laneDistance * laneNumber, Time.deltaTime * sideSpeed);
         transform.position = pos;
         
         moveDirection.y -= gravity * Time.deltaTime;        
         controller.Move (moveDirection * Time.deltaTime);
     }
 }

Comment
Add comment · Show 2 · 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
avatar image Adam-Halley-Prinable · Jan 05, 2016 at 01:40 AM 0
Share

Go into unity and open Edit->project settings->Input

Find the entry that says "Vertical", open it up, find where it says "s" or "downarrow" or whatever the button you're using for backwards is, and delete that "s".

avatar image Dark Age Adam-Halley-Prinable · Jan 05, 2016 at 01:42 AM 0
Share

Awesome thanks $$anonymous$$big kudos to you

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

39 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help about getAxis 0 Answers

Hi, got a problem with my vertical axis. 0 Answers

Using GetAxis with UI Buttons 0 Answers

Input Keyboard GetAxis not working 0 Answers

How can i add vertical move to this script 0 Answers


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