- Home /
Cube rotation and force
Hi guys. I'm trying to create a game similiar to "Ahtung! Die Kurve". I'm trying to get those cubes to rotate with "a" and "d" buttons but it happends very slow. Please help me to get rid of that problem.
Here is my code for movement:
void Update()
{
if (networkView.isMine)
{
InputMovement();
InputColorChange();
}
else
{
SyncedMovement();
}
//===Force===========================
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
//===Movement========================================
public void InputMovement()
{
/*if(Input.GetKey(KeyCode.DownArrow))
transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.LeftArrow))
transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.RightArrow))
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
*/
if(Input.GetKey(KeyCode.W))
transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
if(Input.GetKey(KeyCode.S))
transform.Translate( -1 * Vector3.forward * Time.deltaTime * moveSpeed);
if(Input.GetKey(KeyCode.A))
transform.Rotate(0,-1,0);
if(Input.GetKey(KeyCode.D))
transform.Rotate(0,1,0);
}
Answer by robertbu · Dec 06, 2013 at 05:46 PM
Your code for 'A' and 'D' rotation should just like the you have comment out for LeftArrow and RightArrow. Replace lines 41 - 45 with:
if(Input.GetKey(KeyCode.A))
transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.D)
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
The 'turnSpeed' variable controls the speed of the turn. If 'turnSpeed' is a public variable, then you must make the change in the Inspector. Changing the value in the script will not change the value used by the component. If you've copied and pasted this code, you will need to declare a 'turnSpeed' variable and give it an initial value.
Note you can also use something similar to the code you already have:
if(Input.GetKey(KeyCode.D))
transform.Rotate(0,turnSpeed * Time.deltaTime,0);
Ok i changed value in inspector. Can you tell me what assigning a value in script doesn't work?
When a variable is 'public', then it is visible in the inspector. So if I have:
public int someVariable = 23;
'someVariable' only gets set to 23, at the time the script is attached to the game object. After that, it takes its value from the Inspector. If you go to the game object with your script, you will see a gear icon in the upper right corner. You can click on the gear and select 'reset' if you want all the public variables to be reset to their default values. So if you change public values in the script and want to propagate those values to the Inspector, you can select 'reset'. As an alternative, if you plan on making the changes in the script, just make the variables 'private'.