Taik
Taik

Reputation: 265

Camera follow the player only on his Z axis Unity 3D

I made the script bellow for the camera to follow the player, but I don't want it to follow him on his X axis. So it should follow the player smoothly only on his Z axis. Any suggestions?

Camera Script:

public Transform Ball;
public float SmoothSpeed = 0.125f;
public Vector3 offset;
bool MoveCamera = true;


void Start () {

}


public void FixedUpdate () {

    if(MoveCamera == true){

        Vector3 desiredPosition = Ball.position + offset;
        Vector3 SmoothedPosition = Vector3.Lerp(transform.position, desiredPosition, SmoothSpeed);
        transform.position = SmoothedPosition;
    }


    if (transform.position.z >= 2f)
    {
        MoveCamera = false;
    }
}

Upvotes: 0

Views: 2939

Answers (3)

hatinacat2000
hatinacat2000

Reputation: 145

Anybody in 2019 need a camera to be offset a specific position from their 3d character? Here is an easy solution! Attach this script to the camera you want to follow the player. Select the player as your target in the Unity editor.

This script sets the new position of the camera to

(EDIT: Where did the LaTeX go?)

$v = <targetPosition.x + xOffset, targetPosition.y + yOffset, targetPosition.z + zOffset>$

Benefits of this script:

  • Camera orientation does not change, even if character turns (because camera is not parented to the player).
  • Works regardless of player position

Limitations: - Because camera orientation is fixed, you need to make sure that your character is not obscured by scenery. - This style of camera control is best-suited for a 3rd person orthographic projection.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour
{

    public Transform target;
    Vector3 targetPos;

    void Update()
    {
        targetPos = target.position;
        transform.position = new Vector3(targetPos.x + 10f, targetPos.y + 8.571067812f, targetPos.z - 10f);
        // transform.LookAt(target.transform);
    }
}

My use-case required an orthographic projection. Orthographic angles are usually at a 30 degree angle of declination, so my offset is back 10 units, over 10 units, and up half that hypotenuse (that is, up 10*sqrt2 * 1/2). I also bumped the y-position up by the character's height (1.5 units), which I thought would give a better center for the camera target. This gives a y-offset of about 8.571067812.

Upvotes: 0

Mircea Oprea
Mircea Oprea

Reputation: 89

What about only changing the z component?

public void FixedUpdate () {

    if(MoveCamera == true){

       Vector3 desiredPosition = new Vector3(0, Ball.position.y + offset.y, Ball.position.z + offset.z);
        Vector3 SmoothedPosition = Vector3.Lerp(transform.position, desiredPosition, SmoothSpeed);
        transform.position = SmoothedPosition;
    }


    if (transform.position.z >= 2f)
    {
        MoveCamera = false;
    }
}

Upvotes: 1

Technivorous
Technivorous

Reputation: 1712

    public gameObject toFollow;
    Vector3 camDistance;
    public void Start(){
    camDistance = toFollow.transform.position-Camera.main.position;
    }

    public void Update(){
    Vector3 following = new Vector3(Camera.main.x,Camera.main.z,toFollow.transform.position.z-Camera.main.position.z);

    Camera.main.transform.position = following;

}

i just threw this together so it may require a few tweaks, but it should get you where you need to go. alternatively.... you can add a rigidbody to the camera, and in the inspector tick the boxes for constraints on the x and y axis. this will allow it to move only on the z (if you go this route remember to turn of the gravity for the cameras rigidbody as well)

Upvotes: 0

Related Questions