- Home /
Character controller will not move through 3D space.
Hi I'm making a 3D fps game, I have the character hovering in 3D space. I require him to move through the 3D space towards the direction of the mouse. We are using the "FPSwalker" script to handle movement. We can currently move around the room on a 2D plane, rather than through the Y axis in 3D space e.g. If we look to the roof we don't move in that direction we move forward on the 2D plane. We have a CharacterController attached to the Player. Here is our current script adapted from the "FPSwalker".
if (!grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
if(Input.GetKey (KeyCode.Mouse0))
{
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
print(ray);
var hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist)) {
var targetPoint = ray.GetPoint(hitdist);
targetPosition = ray.GetPoint(hitdist);
print(targetPosition);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
moveDirection = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
}
}
}
Comment