0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectFollowCamera : MonoBehaviour
{
    public Transform cameraToFollow;
    public Transform objectToFollow;
    public float distanceFromCamera;

    private void Update()
    {
        objectToFollow.position = new Vector3(cameraToFollow.position.x,cameraToFollow.position.y,cameraToFollow.position.z - distanceFromCamera);
    }
}

This script is attached to a empty GameObject. The objectToFollow will move once i'm moving the Player with the keys. But when i'm rotating the camera of the player around the objectToFollow is not moving.

Player

The Player have attached to it: Character Controller , First Person Controller , Rigidbody

And the Camera is child of the Player.

\$\endgroup\$
0
\$\begingroup\$

You don't want to immediately just snap the follower's position to the target's position. It won't look as good, and won't function like following in the sense that the follower moves towards the target. You need to use interpolation and rotation to achieve this. These are the steps that I can think of that worked for me:

1) Interpolate the follower's position to some position near the target's position.

2) Take into account any grounds or floors or walls around the follower. You can actually put a second Character Controller on the follower and move that. If you can't use that, then you can always use some different collider to detect collisions. Should work just as fine.

3) Rotate to look at, or look in any direction you would like, while doing number one.

Do this, and you should be good to go!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.