So I finished my first game yesterday, but I came to the conclusion that the player movement doesnt work in the build version. which kinda sucks. How can I solve this issue?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Movement : MonoBehaviour
{
public Rigidbody rb;
public float X_Axis;
public float Z_Axis;
public float force;
public bool Forward_backward;
public bool leftward_rightward;
void Start()
{
GetComponent<Rigidbody>().freezeRotation = true;
Forward_backward = true;
leftward_rightward = true;
force = 5f;
}
public void Update()
{
//Sideward
if (Input.GetKey ("a"))
{
if (leftward_rightward == true)
{
leftward_rightward = false;
Debug.Log("leftward_rightward is false");
}
}
if (Input.GetKey("d"))
{
if (leftward_rightward == false)
{
leftward_rightward = true;
Debug.Log("leftward_rightward is true");
}
}
if (leftward_rightward == true)
{
X_Axis = force;
}
if (leftward_rightward == false)
{
X_Axis = force * -1;
}
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
if (Input.GetKey("w"))
{
rb.AddForce(new Vector3(0f, 0f, Z_Axis));
}
if (Input.GetKey("s"))
{
rb.AddForce(new Vector3(X_Axis, 0f, 0f));
}
Edit: the leftward_rightward bits do work, its just the input of w and s that doesnt work.
To get started and help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong: