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 nicedude80 · Oct 27, 2014 at 10:46 PM · 2dtransformrotaterigidbody2dinput.getaxis

Will not rotate when going different speed?

So, I have a 2d sprite that needs to move forward when the vertical axis is greater then 0. This works fine. I also made it so that the sprite cannot rotate unless moving forward. But now, I want to have the sprite change speed when shift or control is pressed. But here's the problem. When I press shift everything works great, the sprite goes double the speed and can rotate. But when I press control, it cuts the speed in half, like it is supposed to, but I cannot rotate what so ever. Note it is all in C#.

 using UnityEngine;
 using System.Collections;
 
 public class Car : MonoBehaviour {
 
     public float speed;
 
     float actualSpeed;
 
     public float rotationSpeed;
 
     float actualRotationSpeed;
 
     void Start () {
     
     }
 
     void Update () {
         if (Input.GetKey (KeyCode.RightShift)) {
             actualSpeed = speed * 2;
         } else if (Input.GetKey (KeyCode.LeftShift)) {
             actualSpeed = speed * 2;
         } else if (Input.GetKey (KeyCode.RightControl)) {
             actualSpeed = speed / 2;
         } else if (Input.GetKey (KeyCode.LeftControl)) {
             actualSpeed = speed / 2;
         } else {
             actualSpeed = speed;
         }
 
         if (Input.GetAxis ("Vertical") > 0) {
             actualRotationSpeed = rotationSpeed;
         }
         if (Input.GetAxis ("Vertical") < 0) {
             actualRotationSpeed = -rotationSpeed;
         }
         if (Input.GetAxis ("Vertical") == 0) {
             actualRotationSpeed = 0;
         }
     }
     void FixedUpdate(){
         transform.Translate (Vector3.up * Input.GetAxis("Vertical") * actualSpeed);
         transform.Rotate (new Vector3(0, 0, -1), Input.GetAxis("Horizontal") * actualRotationSpeed);
     }
 }
 
Comment
Add comment · Show 4
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 AlwaysSunny · Oct 28, 2014 at 12:15 AM 0
Share

Rotation does not appear to be linked to whether control is pressed in any way. Is there other code involved?

avatar image Habitablaba · Oct 28, 2014 at 12:54 AM 0
Share

Except that bit that uses Input.GetAxis("Vertical").
What do you have bound to that axis? Shift and Control?

avatar image nicedude80 · Oct 29, 2014 at 02:30 AM 0
Share

There is no other scripts for movement. Vertical is "W" and "S" and Up and Down arrow.

avatar image nicedude80 · Oct 29, 2014 at 02:33 AM 0
Share

I wonder is this could be fixed by ins$$anonymous$$d of the last 3 if statements in the Update function could be replaced by checking the transforms speed? Is that possible?

1 Reply

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

Answer by nicedude80 · Oct 29, 2014 at 03:05 AM

I do not know what happened, but it now works. I think it had something to do withy unity recognizing "Control" as a command for the Unity Editor itself. When I run the game on a standalone player, it works fine.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

rigidBody2d.rotation only updating if rigidbody is moving. 1 Answer

Find left joystick Vector2 away from sprite's up direction. 1 Answer

Handling 2D Slope 1 Answer

Dash in the direction of a parents child object 1 Answer

How can I rotate my player to look at the direction my Joystick is pointing to? (Top-Down 2D) 3 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