So im trying to make a 3d character move only on the X,Y axis, but I can only make it move vertically and horizontally, and not diagonally.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public GameObject player;
public float speed;
private Vector3 pos;
// Start is called before the first frame update
void Start()
{
pos = player.transform.position;
}
// Update is called once per frame
void Update()
{
Vector3 current = player.transform.position;
player.transform.position = Vector3.Lerp(current, pos, 10 * Time.deltaTime);
float y = player.transform.position.y;
float x = player.transform.position.x;
float z = player.transform.position.z;
// I suspect the problem is here. Are the lines of code setting 'pos' interfering with each other?
if (Input.GetKey(KeyCode.W))
{
pos = new Vector3(x, y + speed, z);
}
else if (Input.GetKey(KeyCode.A))
{
pos = new Vector3(x - speed, y, z);
}
else if (Input.GetKey(KeyCode.S))
{
pos = new Vector3(x, y - speed, z);
}
else if (Input.GetKey(KeyCode.D))
{
pos = new Vector3(x + speed, y, z);
}
}
}