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 phxvyper · Apr 13, 2014 at 09:31 PM · unity 4slidingkey pressedrelative rotationrelative position

Why isn't my character moving left/right or rotating!

I'm creating a temple-run-like game as a prototype and this is the update() script for it:

 if (PlayerAlive) {
             /*
              * While the player is alive, move at velocities dictated by
              * lane changing (x-velocity) - A/D Left/Right
              * jumping (y-velocity) - W Up --- jumping is not implemented yet
              * forward constant (z-velocity)
              */
             transform.Translate(0f, 0f, this.currentZVelocity * Time.deltaTime, Space.Self);
 
             /*
              * Handle key presses, assigned respectiveley as mentioned prior.
              */ 
             if (Input.GetKeyDown(KeyCode.A | KeyCode.LeftArrow)) {
                 if (Mathf.Approximately(transform.position.x, 0f) ||
                     Mathf.Approximately(transform.position.x, 2f)) {
                     transform.Translate(-2f, 0f, 0f, Space.Self);
                 }
             }
             else if (Input.GetKeyDown(KeyCode.D | KeyCode.RightArrow)) {
                 if (Mathf.Approximately(transform.position.x, 0f) ||
                     Mathf.Approximately(transform.position.x, -2f)) {
                     transform.Translate(2f, 0f, 0f, Space.Self);
                 }
             }
             else if (Input.GetKeyDown(KeyCode.S | KeyCode.DownArrow)) {
                 Slide = true;
             }
 
             /*
              * Apply asthetic rotation and translation for sliding.
              */
             if (Slide && !Sliding) {
                 transform.rotation = new Quaternion(-80f, 0f, 0f, transform.rotation.w);
                 transform.position = new Vector3(transform.position.x, 0.5f, transform.position.z);
                 Camera.main.transform.rotation = new Quaternion(80f, 0f, 0f, Camera.main.transform.rotation.w);
                 Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, 0.5f, Camera.main.transform.position.z);
                 Sliding = true;
             } else if (Slide && Sliding && SlidingTimeDelta < SlidingTime) {
                 SlidingTimeDelta += Time.deltaTime;
             } else if (Slide && (Mathf.Approximately(SlidingTimeDelta, SlidingTime) || SlidingTimeDelta > SlidingTime)) {
                 transform.rotation = new Quaternion(0f, 0f, 0f, transform.rotation.w);
                 transform.position = new Vector3(transform.position.x, 1.5f, transform.position.z);
                 Camera.main.transform.rotation = new Quaternion(0f, 0f, 0f, Camera.main.transform.rotation.w);
                 Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, 4f, Camera.main.transform.position.z);
                 
                 Sliding = false;
                 Slide = false;
                 SlidingTimeDelta = 0f;
             }
         }

The only part of the code that seems to be functioning correctly is the transform.Translate(0f, 0f, this.currentZVelocity * Time.deltaTime, Space.Self); part.

When i press A or D, nothing new happens. Same with if i press S.

However, i have noticed that the x-axis on my character is slowly increasing from 0f. I can imagine this is because of the inaccuracy of floats and i am constantly translating the character to x = 0f relative to Space.Self... This seems easy to fix, though.

So, why is it that my script isn't acting whenever i press A, D or S?

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
1
Best Answer

Answer by robertbu · Apr 13, 2014 at 09:56 PM

The likely cause of your problem is that you are treating KeyCodes as if they are bits that can be combined. You need to change this:

 if (Input.GetKeyDown(KeyCode.A | KeyCode.LeftArrow)) {

To:

 if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) {

I also think the rest of the code inside the clause may have problems. Try something like this instead:

      if ((Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) {
        if (transform.position.x > -0.5f) {
           Vector3 pos = transform.position;
           pos.x = -2.0f;
           transform.position = pos;
        }
      }
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

21 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

Related Questions

transform.right not working correctly? 0 Answers

VR: Relative controller position on local axis 0 Answers

How can I stop Navmesh agent sliding? 6 Answers

How do I make A script to open 1 sliding door and another to open 2 sliding doors? 1 Answer

Sliding down a slope with a character controller 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