using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
private Vector3 pos;
void Start () {
}
void Update () {
if (Input.GetKey("w")) {
pos.z = pos.z + 0.1f;
}
if (Input.GetKeyDown("s")) {
pos.z = pos.z - 0.1f;
}
}
}
The player doesn’t move when I press w or s even though there aren’t any compiler errors.
Whilst you have a Vector3 variable for position, and you change it, you’re never actually telling your objects transform to assume that vector as its position.
try using this update.
void Update ()
{
pos = transform.position;
if (Input.GetKey("w"))
{
pos.z = pos.z + 0.1f;
}
if (Input.GetKeyDown("s"))
{
pos.z = pos.z - 0.1f;
}
transform.position = pos;
}
Keep in mind that this sort of movement will be frame-rate dependant, so it might be worth having a “speed” variable and then multiplying your increment/decrement of the z value by (speed * Time.deltaTime), just a suggestion.