I can't tell what's wrong with this script. I've tried putting all the variables in "void Start()" but nothing's worked. Please help. From what I can tell, everything in the update script isn't the problem.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementScript : MonoBehaviour{ }
{
Rigidbody PlayerCube;
float Jump = 1;
float Speed = 1;
void Update()
{
// All of this has the controls tilted 45 degrees. Deal with it for now.
if (Input.GetKey(KeyCode.W)) ;
{ PlayerCube.AddForce(0, 0, Speed * 5); }
if (Input.GetKey(KeyCode.S)) ;
{ PlayerCube.AddForce(0, 0, Speed * -5); }
if (Input.GetKey(KeyCode.A)) ;
{ PlayerCube.AddForce(Speed * 5, 0, 0); }
if (Input.GetKey(KeyCode.D)) ;
{ PlayerCube.AddForce(Speed * -5, 0, 0); }
if (Input.GetKeyDown(KeyCode.Space)) ;
{ PlayerCube.AddForce(0, (float)(Jump * 2.5), 0); }
}
}
CodePudding user response:
- Remove the pair of parenthesis
{ }
afterMonoBehaviour
- Remove semi-colon
;
after everyif
statement
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementScript : MonoBehaviour
{
Rigidbody PlayerCube;
float Jump = 1;
float Speed = 1;
void Update()
{
// All of this has the controls tilted 45 degrees. Deal with it for now.
if (Input.GetKey(KeyCode.W))
{ PlayerCube.AddForce(0, 0, Speed * 5); }
if (Input.GetKey(KeyCode.S))
{ PlayerCube.AddForce(0, 0, Speed * -5); }
if (Input.GetKey(KeyCode.A))
{ PlayerCube.AddForce(Speed * 5, 0, 0); }
if (Input.GetKey(KeyCode.D))
{ PlayerCube.AddForce(Speed * -5, 0, 0); }
if (Input.GetKeyDown(KeyCode.Space))
{ PlayerCube.AddForce(0, (float)(Jump * 2.5), 0); }
}
}
Modify your Update()
method for simplicity
void Update()
{
PlayerCube.AddForce(-Speed * 5 * Input.GetAxisRaw("Vertical"), 0, Speed * 5 * Input.GetAxisRaw("Horizontal"));
if (Input.GetKeyDown(KeyCode.Space))
{
PlayerCube.AddForce(0, (float)(Jump * 2.5), 0);
}
}
CodePudding user response:
if (Input.GetKey(KeyCode.W)) ;
You are ending your if
condition with the semi-colon at the end of this line. You need to remove it. E.g. -
if (1 != 1) ;
{ Console.WriteLine("???"); }
will print ???
to the console, because I terminated the if statement before moving to the next statement.