hello, I am fairly new to this, and didn’t find another forum post for this, though I may not have done a query well enough to find the correct post. My code is below (found these through various searches).
using UnityEngine;
using System.Collections;
public float playerSpeed;
public float playerSpeedInitial = 1.5f;
public float playerSpeedMax = 10f;
public bool moving;
private bool moveForward;
private bool moveRight;
private bool moveBackward;
private bool moveLeft;
public class Movement : MonoBehaviour` {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
while (moving && playerSpeed != playerSpeedMax){
playerSpeed = playerSpeedInitial + .15f * Time.deltaTime;
}
while (moving && playerSpeed = playerSpeedMax){
playerSpeed = playerSpeedMax;
}
// below is the part that seems to be causing an issue, so I just wanted to
//check if this would work... so basically, can this input change the Boolean
//value of moveForward, and if it can, do I have to modify this to make it work as
//it should? (cant remember how to make multi line comments, so sorry about that.)
if (Input.GetKeyDown(KeyCode.W)){
moveForward = true;
}
if (moveForward){
transform.localPosition += transform.forward * playerSpeed;
moving = true;
}
if (Input.GetKeyDown(KeyCode.D)){
moveRight = true;
}
if (moveRight){
transform.localPosition += transform.right * playerSpeed;
moving = true;
}
if (Input.GetKeyDown(KeyCode.S)){
moveBackward = true;
}
if (moveBackward){
transform.localPosition += transform.backward * playerSpeed;
moving = true;
}
if (Input.GetKeyDown(KeyCode.A)){
moveLeft = true;
}
if (moveLeft){
transform.localPosition += transform.left * playerSpeed;
moving = true;
}
if !moving{
playerSpeed = 0f;
}
}
}
}
You can do this (forgive me for messing up your braces)
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour
{
public float playerSpeed;
public float playerSpeedInitial = 1.5f;
public float playerSpeedMax = 10f;
public bool moving;
private bool moveForward;
private bool moveRight;
private bool moveBackward;
private bool moveLeft;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
/*
// the following could be an endless loop - uncomment it at your own risk (save scene first
while (moving && playerSpeed != playerSpeedMax)
{
playerSpeed = playerSpeedInitial + .15f * Time.deltaTime;
}
*/
/*
// the following is an endless loop - un comment it at your own risk (save scene first
while (moving && playerSpeed == playerSpeedMax)
{
playerSpeed = playerSpeedMax;
}
}
*/
moveForward = (Input.GetKeyDown(KeyCode.W));
moveRight = (Input.GetKeyDown(KeyCode.D));
moveBackward = (Input.GetKeyDown(KeyCode.S));
moveLeft = (Input.GetKeyDown(KeyCode.A));
moving = (moveForward || moveRight || moveBackward || moveLeft);
if (moveForward)
{
transform.localPosition += transform.forward * playerSpeed;
}
if (moveRight)
{
transform.localPosition += transform.right * playerSpeed;
}
if (moveBackward)
{
transform.localPosition += transform.backward * playerSpeed;
}
if (moveLeft)
{
transform.localPosition += transform.left * playerSpeed;
}
if (!moving)
{
playerSpeed = 0f;
}
}
}
or you can do this
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour
{
public float playerSpeed;
public float playerSpeedInitial = 1.5f;
public float playerSpeedMax = 10f;
public bool moving;
private bool moveForward;
private bool moveRight;
private bool moveBackward;
private bool moveLeft;
// Use this for initialization
void Start ()
{
}
public void OnMouseDown ()
{
moveForward = (Input.GetKeyDown(KeyCode.W));
moveRight = (Input.GetKeyDown(KeyCode.D));
moveBackward = (Input.GetKeyDown(KeyCode.S));
moveLeft = (Input.GetKeyDown(KeyCode.A));
moving = (moveForward || moveRight || moveBackward || moveLeft);
if (moveForward)
{
transform.localPosition += transform.forward * playerSpeed;
}
if (moveRight)
{
transform.localPosition += transform.right * playerSpeed;
}
if (moveBackward)
{
transform.localPosition += transform.backward * playerSpeed;
}
if (moveLeft)
{
transform.localPosition += transform.left * playerSpeed;
}
if (!moving)
{
playerSpeed = 0f;
}
}
// Update is called once per frame
void Update ()
{
/*
// the following could be an endless loop - uncomment it at your own risk (save scene first
while (moving && playerSpeed != playerSpeedMax)
{
playerSpeed = playerSpeedInitial + .15f * Time.deltaTime;
}
*/
/*
// the following is an endless loop - uncomment it at your own risk (save scene first
while (moving && playerSpeed == playerSpeedMax)
{
playerSpeed = playerSpeedMax;
}
}
*/
}
First of you had this for one of your while loops
while (moving && playerSpeed = playerSpeedMax)
this
playerSpeed = playerSpeedMax
is an assignment, not a comparison. You either wanted this
playerSpeed != playerSpeedMax
or this
playerSpeed == playerSpeedMax
Second, both while loops have the potential to be endless loops. I commented them out. You should only uncomment them (one at a time) after saving your scene.