Other
How do move a player on an xz plane with a top down orbit camera?
Hello world,
so in my game I have a 3rd person camera that is looking down upon my player and has the ability to orbit around him/her when holding down the right mouse button. (granted I cant change any of the offsets when in game, another problem to work on)
public class cameraController : MonoBehaviour {
public float turnSpeed = 4.0f;
public Transform player;
public float zOffset = 7.0f;
public float yOffset = 8.0f;
private Vector3 offset;
void Start() {
offset = new Vector3(player.position.x, player.position.y + yOffset, player.position.z + zOffset);
transform.position = player.position + offset;
}
void LateUpdate () {
if(Input.GetMouseButton(1)) {
offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
}
transform.position = player.position + offset;
transform.LookAt(player.position);
}
}
Using this camera controller I want to be able to move the player using the WASD keys depending on the camera. This is what I got.
public class playerController : MonoBehaviour {
public float moveSpeed;
private Rigidbody rb;
private Camera playerCamera;
private Vector3 keyboardInput;
private Vector3 playerDirection;
private Vector3 moveVelocity;
private int cameraLayermask;
// Use this for initialization //
void Start() {
rb = GetComponent<Rigidbody>();
playerCamera = FindObjectOfType<Camera>();
// Layers for Camera Raycast to focus on //
int groundLayer = 8;
// Add layers to Layermask
cameraLayermask = 1 << groundLayer;
}
// Update is called once per frame //
void Update() {
keyboardInput = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
playerDirection = playerCamera.transform.TransformDirection(keyboardInput);
playerDirection.y = 0f;
print(playerDirection);
moveVelocity = playerDirection * moveSpeed;
if (!Input.GetMouseButton(1)) {
Ray cameraRay = playerCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(cameraRay, out hit, Mathf.Infinity, cameraLayermask)) {
Debug.DrawLine(cameraRay.origin, hit.point, Color.blue);
transform.LookAt(new Vector3(hit.point.x, hit.point.y + 0.5f, hit.point.z));
}
}
}
void FixedUpdate() {
//Replace Gravity
moveVelocity.y = rb.velocity.y;
rb.velocity = moveVelocity;
}
}
Since I'm using
playerCamera.transform.TransformDirection(keyboardInput)
the cube is going to want to get pushed into the ground, in order to prevent this I added
playerDirection.y = 0f
this however has lead to a very slow moving forward motion.
What can I do to fix this?
I just used vector3.Normalize and while it "technically" works the movement was not as smooth as as when without it. Hopefully there is a better solution.
Follow this Question
Related Questions
Can someone help me explain this Script here :) ? Help would be greatly appreciated 0 Answers
Return First person Rig to Start 0 Answers
falling script flaws 0 Answers
Help!--How to make player follow finger touch position in Space Shooter? 0 Answers
How to jump between -1 ,0 ,1 on x axis, using A and D keys? 0 Answers