- Home /
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: this 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 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 * accel); //this adds force relative to it's own transform
}
}
Answer by RensDevolp · Dec 22, 2017 at 11:04 AM
Try this modified script to see if that works:
float rotation = Input.GetAxis("Horizontal") * turnSpeed; //this gives you an rotation: either -1 * turnspeed or 1*turnspeed
if (onCooldown == false && boostLeft < 100)
{
boostLeft += 1f*Time.deltaTime; //do this 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 this, 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 this:
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
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.
also how does it know to use left and right arrows now??
I made a new script for you:
public float turnSpeed = 10;
public float speed = 10;
public float boost$$anonymous$$ultiplier = 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.Get$$anonymous$$ey ($$anonymous$$eyCode.Space)) { //boost now
boosting = true;
}
if (boosting && Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.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 * boost$$anonymous$$ultiplier;
} 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: $$anonymous$$y rx580 can run the game at 600 fps. That means that the value is increased with 600 every second. $$anonymous$$y 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
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.
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 boost$$anonymous$$ultiplier = 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.Get$$anonymous$$ey($$anonymous$$eyCode.Space) && boost > 0 )
{ //boost now
boosting = true;
boost -= 1;
}
if (boosting && Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.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 * boost$$anonymous$$ultiplier;
}
else
{
//else just use the normal speed
tempSpeed = speed;
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow))
{
rb.AddRelativeForce(Vector3.forward * tempSpeed); //this adds force relative to it's own transform
}
}
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.
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.
Please go to https://docs.unity3d.com/$$anonymous$$anual/class-Input$$anonymous$$anager.html . It will give you an explanation about how to use inputs
Your answer
Follow this Question
Related Questions
Infinite Road Clone Duplication 1 Answer
Drive car with Pedals 0 Answers
Better RPM?? 0 Answers
Combining if Statements 1 Answer
Single line if statement with many conditions or several single-conditioned if statements 1 Answer