I’m trying to walk where my character looks.
What I’m trying to do is change my camera’s z rotation with the mouse, and then set the rotation of the player to the camera’s z rotation. I’ve been trying for an hour or so, and I can’t get it to work. There are no errors.
Here is my code if you need a look.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class Player : NetworkBehaviour
{
public Canvas canvas;
public Joystick joystickL;
public Joystick joystickR;
public override void OnStartLocalPlayer() {
Camera.main.transform.SetParent(transform);
Camera.main.transform.localPosition = new Vector3(0, 0, 0);
if(Application.platform == RuntimePlatform.Android) {
canvas.GetComponent<Canvas>().enabled = true;
}
}
void Update() {
float moveX;
float moveZ;
float lookY;
float lookX;
if(!isLocalPlayer) { return; }
if(Application.platform == RuntimePlatform.Android) {
moveX = joystickL.Horizontal * Time.deltaTime * 110.0f;
moveZ = joystickL.Vertical * Time.deltaTime * 4f;
lookY = joystickR.Vertical * Time.deltaTime * 110.0f;
lookX = joystickR.Horizontal * Time.deltaTime * 110.0f;
} else {
moveX = Input.GetAxis("Horizontal") * Time.deltaTime * 110.0f;
moveZ = Input.GetAxis("Vertical") * Time.deltaTime * 4f;
lookX = Input.GetAxis("Mouse X") * Time.deltaTime * 110.0f;
lookY = -(Input.GetAxis("Mouse Y")) * Time.deltaTime * 55.0f;
}
Camera.main.transform.Rotate(0, lookX, 0);
transform.rotation = Quaternion.Euler(0, Camera.main.transform.rotation.y, Camera.main.transform.rotation.z);
transform.Translate(0, 0, moveZ);
Camera.main.transform.Rotate(lookY, 0, 0);
}
}
Any help would be great.