- Home /
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);
}
}
Rotation does not appear to be linked to whether control is pressed in any way. Is there other code involved?
Except that bit that uses Input.GetAxis("Vertical").
What do you have bound to that axis? Shift and Control?
There is no other scripts for movement. Vertical is "W" and "S" and Up and Down arrow.
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?
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.
Your answer
Follow this Question
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