Player Camera Help

So i just started my own game and ive played my player move and The camera go to the player in 3rd person. Now i have a problem where im trying to make it so i can rotate the camera around the player while moving, global camera rotation etc what ever you call it. ive tried everything from youtube, to documentations and even udemy course example and i just cant get it. this is my game.

now this is my Code:

public class CameraFollow : MonoBehaviour
{
    [SerializeField] Transform target;

    [SerializeField] float smoothspd = 0.125f;
    [SerializeField] Vector3 offset;

    void FixedUpdate()
    {
        Vector3 desiredPosition = target.position + offset;
        Vector3 smoothing = Vector3.Lerp(transform.position, desiredPosition, smoothspd);
        transform.position = smoothing;

        transform.LookAt(target);

    }

The Crazy Rotation you see is me when i tried using speed with euler Angles and it only sped up the players rotation but i took that off. as for as everything else its the same.

Hi Jayy,

Where is the code for the rotation?

What you could use is Transform.RotateAround. You know the player’s transform.position, so you can simple pass it on to the method.

1 Like

yeah i tried that with the code i had and the camera was fighting with the code i already had.
what seems to be the problem with my code is that the camera is following the player but slowley because i wanted to look like that but it doesnt seem like youre supposed to do that with 3rd person games.
Another thing is i could simply just scratch some of the code i have now and parent the player and put the camera on it but the player is a ball and it rolls and i want it to therefor the camera rolls around. i just want to be able to move around the way i am in the video put have the direction of my camera be the forward direction. I tried putting a small sphere inside the ball that doesnt rotate but i really just lack the knowledge on rotations and angles that i didnt know how even after watching countless videos. hope this helps

@Jayy_Ron
What behaviour are you after exactly? For the camera to rotate around the view of the ball as you use A and D keys to turn?

Ah just seen its mouse control dependant.

well idealy i wanted to be able to move the camera around the player using the axises for the Mouse X and Y And have the ball move in the direction the camera is facing but it doesnt seem like i can or seems like to much so having the ball move in the direction of the camera is good enough it doesnt need to rotate

like you know how in assasins creed if you have the camera looking behind you you then walk behind you when you press forward. like that. sry if what im asking is confusing

RotateAround would probably be your easiest solution.
Or try something like this

public GameObject Player;
float distance = 10.0f;
float maxDistance = 12.5f;
float minDistance = 5.0f;
private float currentX = 0.0f;
private float rotationSpeed = 2.0f;
private float zoomSpeed = 0.2f;
private Camera mainCamera;

void Start()
{
    Player = GameObject.FindWithTag("Player");
    mainCamera = Camera.main;
}

void Update()
{
    if (Input.GetMouseButton(2))
    {
        currentX += Input.GetAxis("Mouse X");
    }
    if (Input.GetAxis("Mouse ScrollWheel") > 0f)
    {
        if (distance >= minDistance)
            distance -= zoomSpeed;
    }
    if (Input.GetAxis("Mouse ScrollWheel") < 0f)
    {
        if(distance <= maxDistance)
            distance += zoomSpeed;
    }
}

void LateUpdate()
{
    Vector3 direction = new Vector3(0, 0, -distance);
    Quaternion rotation = Quaternion.Euler(45.0f, currentX * rotationSpeed, mainCamera.transform.position.z);
    mainCamera.transform.position = Player.transform.position + rotation * direction;
    mainCamera.transform.LookAt(Player.transform.position);
}

Isnt this movement for clicking? im just asking because i see get mousebutton. Mine is with keys.

private void PlayerMovement()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(horizontalInput, 0, verticalInput);       
        Vector3 velocity = movement * playerspd;
        
        rb.AddForce(velocity * Time.deltaTime);

    }

No its holding the mouse button in and moving the mouse rotates the camera around the ball.
Its not exactly what you need i think but with a bit of work it might.
I suggest just putting it on the camera and seeing how it reacts :slight_smile:

(Or at least i think it does if i posted the right script :confused: long day and on phone eating dinner :wink: )

Ill give it a try

alright so i tried it and it rotates when i hold down the scroll button. but it doesnt move in the direction the camera is facing.

   private void FixedUpdate() 
    {
        ApplyControls();
        FacingAngle();
        //Apply force behind ball should always be fixedupdate as physics
        this.transform.GetComponent<Rigidbody>().AddForce(new Vector3(unitv2.x, 0 , unitv2.y) * z * 3);   
        // Set Camera Position behind the ball based on its rotation
        cameraReference.transform.position = new Vector3(-unitv2.x * distanceToCamera, distanceToCamera, -unitv2.y * distanceToCamera) + this.transform.position; 
    }
    
    private void FacingAngle()
    {
        // Facing Angle.
        facingAngle += x;
        unitv2 = new Vector2(Mathf.Cos(facingAngle * Mathf.Deg2Rad), Mathf.Sin(facingAngle * Mathf.Deg2Rad));
    }

    private void ApplyControls()
    {
        // User controls
        x = Input.GetAxis("Horizontal") * Time.deltaTime * horizontalSpeed;
        z = Input.GetAxis("Vertical") * Time.deltaTime * verticalSpeed;
        Jump();
    }

    private void Jump()
    {
        if (isGrounded && Input.GetKeyDown(KeyCode.Space))
        {
            rbody.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
            isGrounded = false;
        }
    }

Probably something more like this then, There is a Katamari style game on Udemy for about £10 that would cover this though

yeah i seen. prob gonna try it seeing as theres alot of mathf going on in the code

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms