How Use rigidbody.MovePosistion With A Player Controlled Moving Camera
Hey guys I am having trouble with some third person movement I am trying to implement. I am using the rigidbody.MovePosition function. In my game that player can use the WASD keys to move all directions and can use the keys q and e to rotate the camera around a focal point I have setup. I am trying to make so that my player can move in the same direction as the camera as well as moving diagonally (Holding the up key and the right key at the same time to go at an angle). I have been able to do these things seperate but can't seem to figure out how I would use the MovePosistion to do them both together. Here is the code for my player controller.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float horizontalInput;
public float verticalInput;
[SerializeField] float speed = 1;
public Vector3 forceVector;
public Rigidbody playerRb;
public GameObject focalPoint;
// Start is called before the first frame update
void Start()
{
focalPoint = GameObject.Find("FocalPoint");
}
// Update is called once per frame
void Update()
{
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
// Code that tries to make so that you can walk diagnol while also having the camera follow you
/*
forceVector = new Vector3(horizontalInput, 0, verticalInput);
forceVector *= speed * Time.fixedDeltaTime;
forceVector = Vector3.Scale(forceVector, focalPoint.transform.right);
forceVector = Vector3.Scale(forceVector, focalPoint.transform.forward);
*/
}
void FixedUpdate()
{
playerRb.MovePosition(transform.position + forceVector);
if (horizontalInput != 0)
{
playerRb.MovePosition(transform.position + (focalPoint.transform.right * horizontalInput * speed).normalized);
}
if (verticalInput != 0)
{
playerRb.MovePosition(transform.position + (focalPoint.transform.forward * verticalInput * speed).normalized);
}
// playerRb.AddForce(transform.right * speed * horizontalInput);
// playerRb.AddForce(transform.forward * speed * verticalInput);
}
}
As shown in this code I have tried a few things and haven't been able to get it to work. The commented code was me trying to come up with the Vector Math to solve the issue. I have the two MovePosistions separate in the FixedUpdate(). This doesn't work for me because you can't walk diagonal you can only walk one direction at a time. I am trying to come up with some vector math that can combine the players input and the direction of the camera.
Here is my camera controller class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public GameObject player;
public Vector3 offset;
public float rotationSpeed;
public float horizontalCamMovement;
public float verticalCamMovement;
// Start is called before the first frame update
void Start()
{
offset = player.transform.position - transform.position;
}
// Update is called once per frame
void Update()
{
transform.position = player.transform.position - offset;
horizontalCamMovement = Input.GetAxis("HorizontalCamMovement");
}
private void FixedUpdate()
{
moveCamera();
}
void moveCamera()
{
if (horizontalCamMovement > 0)
{
Debug.Log("You are pressing the E key");
transform.Rotate(Vector3.up, horizontalCamMovement * Time.deltaTime * rotationSpeed);
}
if (horizontalCamMovement < 0)
{
Debug.Log("You are pressing the q key");
transform.Rotate(Vector3.up, horizontalCamMovement * Time.deltaTime * rotationSpeed);
}
}
}
I have seen some solutions online but can't seem to understand where the code comes from. If anyone could help me out and give me a detailed explanation so that I could learn I would really appreciate it. If you have any questions about my question be sure and let me know and I will answer them :) Thank You
Your answer
Follow this Question
Related Questions
When attaching my custom camera script camera shakes when player starts to move fast. 0 Answers
Help about understanding ceratin command problem 1 Answer
I want my camera to move only Y axis and my character to rotate with my camera 0 Answers
When attaching my custom camera script camera shakes when player starts to move fast. 1 Answer
Does anyone know the script for a smooth camera follow of the main game object? 8 Answers