Adding variable offset to camera

Hi,

i have a game where you have to move the player left and right to avoid hitting obstacles.
The player and the camera get moved in the same way so that it looks like the player is a child of the camera. So there is a offset between the player and the camera.
Now i want that the offset becomes smaller when the player hits obstacles.
How can i do this with my code?

The player gets moved by this class:

using UnityEngine;

public class Move : MonoBehaviour {

    [Range(1, 2000)]
    public float speed;

    [Range(1, 2000)]
    public static float speedFactor;

    private float ScreenWidth;

    //Distance between the player and the camera
    public static Vector3 offset;

    // Use this for initialization
    void Start() {
        ScreenWidth = Screen.width;
        speed = 10f;
        speedFactor = 1.2f;
    }
    // Update is called once per frame
    void Update() {   
        move();
        moveMP();
    }
  void move() {

        gameObject.transform.position += Vector3.forward * speed * speedFactor * Time.deltaTime;

        //On PC
        if (Input.GetKey(KeyCode.A)) {
            gameObject.transform.position += Vector3.left * speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.S)) {
            gameObject.transform.position += Vector3.back * speed * Time.deltaTime;
        }

and the camera gets moved in the same way by this class:

using UnityEngine;

public class MoveCamera : MonoBehaviour
{
    [Range(1, 2000)]
    public float speed;

    public static float speedFactor = 1.2f;

    public int ScreenWidth;

    // Use this for initialization
    void Start()
    {
        ScreenWidth = Screen.width;
        speed = 10f;
        speedFactor = 1.2f;
    }
    // Update is called once per frame
    void Update()
    {
        move();
        moveMP();
    }
    private void LateUpdate()
    {
        
    }
    void move()
    {
        gameObject.transform.position += Vector3.forward * speed * speedFactor * Time.deltaTime;

        //On PC
        if (Input.GetKey(KeyCode.A))

        {
            gameObject.transform.position += Vector3.left * speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.S))
        {
            gameObject.transform.position += Vector3.back * speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.D))

        {
            gameObject.transform.position += Vector3.right * speed * Time.deltaTime;

        }
    }

How can i add the offset to this line:

gameObject.transform.position += Vector3.forward * speed * speedFactor * Time.deltaTime;

This is of course not working:

gameObject.transform.position += Vector3.forward * speed * speedFactor * Time.deltaTime + Hit.offset;

Hit.offset is of the type Vector3

When i have to this in an other way please tell me.

Thank you

Hello, in the update of the camera do this:

transform.position = player.position + offset;

with this you only move the player with the input and the position of the camera is set from the player’s position.
Raph

The red cube is the player and consist of 125 smaller cubes. So i cant use the player as target because the origin of the player is changing every time he hits an obstacle.

After hitting obstacles the camera gets closer

I already had a gameobject (cameraPlayer) which got moved by the MovingCamera script.
The camera was following the cameraplayer:

  gameObject.transform.position = CameraPlayer.transform.position + Hit.offset;

The problem was that this had lead to an asymmetry between the player and the camera, because the player was already at the next position but the camera wasn’t. So it looked like the player (the red cube) were shaking. So your solution is not working for me @WhiteCry but thank you anyway :slight_smile: