- Home /
Question by
Tibllec · Mar 12 at 01:59 AM ·
cameramovementscripting beginnermovement scriptspace
How do i change the player camera to a free view?
I'm making a game similar to outer wilds where you can go to other planets, I'm doing fine with the scripts, but the biggest problem here is player movement in space.
I don't know of a way to define a player's movement in space, where you can freely rotate and go in the direction you're facing.
need help here
public class FirstPersonController : MonoBehaviour
{
public GravityOrbit Gravity;
// public vars
public float mouseSensitivityX = 1;
public float mouseSensitivityY = 1;
public float walkSpeed = 6;
public float jumpForce = 220;
public LayerMask groundedMask;
// System vars
bool grounded;
Vector3 moveAmount;
Vector3 smoothMoveVelocity;
float verticalLookRotation;
Transform cameraTransform;
Rigidbody rigidbody;
public float ZeroG = 5;
void Awake()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
cameraTransform = Camera.main.transform;
rigidbody = GetComponent<Rigidbody>();
}
void Update()
{
if (Gravity)
{
// Look rotation:
transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX);
verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY;
verticalLookRotation = Mathf.Clamp(verticalLookRotation, -60, 60);
cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
// Calculate movement:
float inputX = Input.GetAxisRaw("Horizontal");
float inputY = Input.GetAxisRaw("Vertical");
Vector3 moveDir = new Vector3(inputX, 0, inputY).normalized;
Vector3 targetMoveAmount = moveDir * walkSpeed;
moveAmount = Vector3.SmoothDamp(moveAmount, targetMoveAmount, ref smoothMoveVelocity, .15f);
// Jump
if (Input.GetButtonDown("Jump"))
{
if (grounded)
{
rigidbody.AddForce(transform.up * jumpForce);
}
}
// Grounded check
Ray ray = new Ray(transform.position, -transform.up);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask))
{
grounded = true;
}
else
{
grounded = false;
}
}
else
{
}
}
public void FixedUpdate()
{
// Apply movement to rigidbody
Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
rigidbody.MovePosition(rigidbody.position + localMove);
}
}
Comment