• 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
Question by Bob_the_dj · Dec 22, 2017 at 08:03 AM · if-statementscar gamedrivingboost

unity boosing car stops left rotation from working

i made a movement script with a limited boost function but when i press space and forward i cant use the left arrow key??

Edit: t$$anonymous$$s code works but must use WASD

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class slipperyDrive : MonoBehaviour
 {
     public float turnSpeed = 10;
     public float speed = 10;
     public float boostMultiplier = 3;
     public float boostCooldown = 5;
     public float boostDecrease = 20;
     public float boostIncrease = 10;
     private bool onCooldown, boosting;
     public float boost = 100;
     private float cooldownTimer;
     public Rigidbody rb;
     // Use t$$anonymous$$s for initialization
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         float rotation = Input.GetAxis("Horizontal") * turnSpeed; //t$$anonymous$$s gives you an rotation: either -1 * turnspeed or 1*turnspeed
         float accel = Input.GetAxis("Vertical"); //The throttle input
 
         if (onCooldown == false && boost < 100 && boosting) //if we aren't boosting, the boost is not full and we aren't on cooldown, increase the boost with boostincrease per sec. 
         {
             boost += boostIncrease * Time.deltaTime;
         }
 
         if (cooldownTimer >= boostCooldown) //the cooldown is over then
         {
             onCooldown = false;
             cooldownTimer = 0;
         }
 
 
         if (Input.GetKey(KeyCode.Space))
         { //boost now
             boosting = true;
         }
 
         if (boosting && Input.GetKeyUp(KeyCode.Space))
         { //if we stopped boosting, set the cooldown
             boosting = false;
             onCooldown = true;
         }
 
         if (onCooldown)
         { //add to the cooldowntimer
             cooldownTimer += Time.deltaTime;
         }
         //and now for the movement:
         //rotate the car. 
        
         transform.Rotate(0, rotation, 0);
         //set a tempspeed
         float tempSpeed;
         //if we are boosting, set the speed to the speed * the multiplier
         if (boosting)
         {
             tempSpeed = speed * boostMultiplier;
         }
         else
         {
             //else just use the normal speed
             tempSpeed = speed;
         }
         rb.AddRelativeForce(Vector3.forward * tempSpeed * accel);  //t$$anonymous$$s adds force relative to it's own transform
     }
 
 
 
 }
 
 
Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by RensDevolp · Dec 22, 2017 at 11:04 AM

Try t$$anonymous$$s modified script to see if that works:

         float rotation = Input.GetAxis("Horizontal") * turnSpeed; //t$$anonymous$$s gives you an rotation: either -1 * turnspeed or 1*turnspeed
 
  
          if (onCooldown == false && boostLeft < 100)
          {
              boostLeft += 1f*Time.deltaTime; //do t$$anonymous$$s for framerate-independent increase. You had +1 every frame, now it is +1 every second
          }
          if (onCooldown == true && boosting == false)
          {
              cooldownTime += 1f*Time.deltaTime;//same story
  
          }
          if (cooldownTime >= 50)
          {
              onCooldown = false;
              cooldownTime = 0;
          }
          
              
              if (Input.GetKey(KeyCode.Space))
              {
                  boosting = true;
                  onCooldown = true;
                
              }
          if (Input.GetKeyUp(KeyCode.Space))
          {
              boosting = false;
              
          }
         /*
         no need to do t$$anonymous$$s, you already have a rotation. 
          if (Input.GetKey(KeyCode.LeftArrow))
          {
              // rb.AddTorque(transform.up * turnSpeed *turn);
              transform.Rotate(0, rotation, 0);
  
          }
          
          if (Input.GetKey(KeyCode.RightArrow))
          {
              transform.Rotate(0, rotation - 360, 0);
              //  rb.AddTorque(transform.up * turnSpeed * turn);
          }
         */
 //instead, you should do t$$anonymous$$s:
  transform.Rotate(0, rotation, 0);
          if (boosting == false)
          {
              if (Input.GetKey(KeyCode.UpArrow))
              {
                  rb.AddForce(transform.forward * speed);
              }
          }
  
          if (boosting == true)
          {
              if (Input.GetKey(KeyCode.UpArrow))
              {
                  rb.AddForce(transform.forward * boostSpeed);
                 
              }
              if (boostLeft > 0)
              {
                  boostLeft -= 1f *Time.deltaTime;
              }
              else
                  {
                  boosting = false;
                  }
          }

you should modify the values: it has a cooldowntime of 50 sec. now, and 1 boost increase and decrease per second.

Rens

Comment

People who like this

0 Show 10 · 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 Bob_the_dj · Dec 23, 2017 at 09:30 AM 0
Share

Thanks for helping, and especially for the clear comments as this is the first game I'm making from scratch by myself (no tutorials - just unity documentation)

sadly, the rotation still seems to be locked in only the LEFT direction when I hold down space and forward arrow.

I'm going to try a different method that uses a separate drive method for boost and normal - ill post it here when that's done along with how it went.

PS. I had the values being removed every frame like that because I have a UI bar the transforms with it and using seconds makes it jump to values, after you explained that it was per frame however will that mean if framerates are different it will recharge at a different speed? how can I fix that without using Fixed Update because that sometimes does not pick up on fast keypresses.

avatar image Bob_the_dj · Dec 23, 2017 at 09:40 AM 0
Share

also how does it know to use left and right arrows now??

avatar image RensDevolp · Dec 23, 2017 at 10:05 AM 0
Share

I made a new script for you:

 public float turnSpeed = 10;
     public float speed = 10;
     public float boostMultiplier = 3;
     public float boostCooldown = 5;
     public float boostDecrease = 20;
     public float boostIncrease = 10;
     private bool onCooldown, boosting;
     private float boost = 100;
     private float cooldownTimer;
     public Rigidbody rb;
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         float rotation = Input.GetAxis("Horizontal") * turnSpeed; //this gives you an rotation: either -1 * turnspeed or 1*turnspeed
         float accel = Input.GetAxis("Vertical"); //The throttle input
 
         if (onCooldown == false && boost < 100 && boosting) //if we aren't boosting, the boost is not full and we aren't on cooldown, increase the boost with boostincrease per sec. 
         {
             boost += boostIncrease*Time.deltaTime;
         }
 
         if (cooldownTimer >= boostCooldown) //the cooldown is over then
         {
             onCooldown = false;
             cooldownTimer = 0;
         }
 
 
         if (Input.GetKey (KeyCode.Space)) { //boost now
             boosting = true;
         }
 
         if (boosting && Input.GetKeyUp (KeyCode.Space)) { //if we stopped boosting, set the cooldown
             boosting = false;
             onCooldown = true;
         }
 
         if (onCooldown) { //add to the cooldowntimer
             cooldownTimer += Time.deltaTime;
         }
         //and now for the movement:
         //rotate the car. 
         transform.Rotate(0, rotation, 0);
         //set a tempspeed
         float tempSpeed;
         //if we are boosting, set the speed to the speed * the multiplier
         if (boosting) {
             tempSpeed = speed * boostMultiplier;
         } else {
             //else just use the normal speed
             tempSpeed = speed;
         }
         rb.AddRelativeForce(Vector3.forward * tempSpeed); //this adds force relative to it's own transform
     }

  

For the time.deltatime, a small example: My rx580 can run the game at 600 fps. That means that the value is increased with 600 every second. My brother with his intel hd graphics can run the same game at 30 fps. His boost etc. will increase 30 per second. Using time.deltatime will increase the value with the value/ current framerate. so that means 1/600 x 600 frames = 1 per sec, and 1/30 x 30 = 1 per sec. time.deltatime is a very small number (1/last frame time). It will still increase smoothly.

for your last comment: input.getaxis returns a value of -1 to 1. So full left is -1, whilst full right is 1.

regards,

Rens

avatar image RensDevolp RensDevolp · Dec 23, 2017 at 10:16 AM 0
Share

Please let me know if this worked. It worked for me. What are your rigidbody's settings? I have disabled the rotations on all axis.

avatar image Bob_the_dj RensDevolp · Dec 23, 2017 at 10:28 AM 0
Share

The object just continued to move forward and was not controlled by the forward button, interestingly it was able to turn correctly. after I made some edits it was working fine other than the original issue

here is the working code based of what you just gave me. public float turnSpeed = 10; public float speed = 10; public float boostMultiplier = 3; public float boostCooldown = 5; public float boostDecrease = 20; public float boostIncrease = 10; private bool onCooldown, boosting; public float boost = 100; private float cooldownTimer; public Rigidbody rb; // Use this for initialization void Start() {

     }
 
     // Update is called once per frame
     void Update()
     {
         float rotation = Input.GetAxis("Horizontal") * turnSpeed; //this gives you an rotation: either -1 * turnspeed or 1*turnspeed
         float accel = Input.GetAxis("Vertical"); //The throttle input
 
         if (onCooldown == false && boost < 100 && boosting == false) //if we aren't boosting, the boost is not full and we aren't on cooldown, increase the boost with boostincrease per sec. 
         {
             boost += boostIncrease * Time.deltaTime;
         }
 
         if (cooldownTimer >= boostCooldown) //the cooldown is over then
         {
             onCooldown = false;
             cooldownTimer = 0;
         }
 
 
         if (Input.GetKey(KeyCode.Space) && boost > 0 )
         { //boost now
             boosting = true;
             boost -= 1;
         }
 
         if (boosting && Input.GetKeyUp(KeyCode.Space))
         { //if we stopped boosting, set the cooldown
             boosting = false;
             onCooldown = true;
         }
 
         if (onCooldown)
         { //add to the cooldowntimer
             cooldownTimer += Time.deltaTime;
         }
         //and now for the movement:
         //rotate the car. 
         transform.Rotate(0, rotation, 0);
         //set a tempspeed
         float tempSpeed;
         //if we are boosting, set the speed to the speed * the multiplier
         if (boosting)
         {
             tempSpeed = speed * boostMultiplier;
         }
         else
         {
             //else just use the normal speed
             tempSpeed = speed;
         }
 
         if (Input.GetKey(KeyCode.UpArrow))
         {
             rb.AddRelativeForce(Vector3.forward * tempSpeed); //this adds force relative to it's own transform
         }
     }
avatar image RensDevolp Bob_the_dj · Dec 23, 2017 at 10:31 AM 0
Share

Yeah I see i made an error posting the code here. This: rb.AddRelativeForce(Vector3.forward * tempSpeed); is supposed to be: rb.AddRelativeForce(Vector3.forward * tempSpeed * accel); Stupid me.

Show more comments
avatar image

Answer by Bob_the_dj · Dec 23, 2017 at 11:22 AM

The script in the post now works but it must use WASD and not the arrow keys so I'm still looking for a explanation.

Comment

People who like this

0 Show 1 · 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 RensDevolp · Dec 23, 2017 at 12:59 PM 0
Share

Please go to https://docs.unity3d.com/Manual/class-InputManager.html . It will give you an explanation about how to use inputs

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

72 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 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

Infinite Road Clone Duplication 1 Answer

Drive car with Pedals 0 Answers

Better RPM?? 0 Answers

How can I change a scene by the rotation z position?, 1 Answer

Why my function not working with less than velocity?,why my function not work with an If statements with a greater/less than 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