简体   繁体   English

相机仅在玩家的Z轴上跟随玩家Unity 3D

[英]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. 我使脚本跟随摄像机跟随玩家,但我不希望它跟随他的X轴。 So it should follow the player smoothly only on his Z axis. 因此,它只能在玩家的Z轴上平稳地跟随玩家。 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;
    }
}

What about only changing the z component? 只更改z分量呢?

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;
    }
}
    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. 或者...。您可以向相机添加刚体,然后在检查器中选中x和y轴上的约束框。 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) 这将使其仅在z轴上移动(如果您沿此路线行驶,请记住也要为相机刚体转动重力)

Anybody in 2019 need a camera to be offset a specific position from their 3d character? 2019年的任何人都需要相机使其3D角色偏离特定位置吗? 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. 在Unity编辑器中选择播放器作为目标。

This script sets the new position of the camera to 该脚本将摄像机的新位置设置为

(EDIT: Where did the LaTeX go?) (编辑:LaTeX到哪里去了?)

$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). 正射角通常为30度的倾斜角,因此我的偏移量是后10个单位,超过10个单位,并且上斜边的一半(即,上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. 我也将y位置增加了角色的高度(1.5个单位),我认为这将为相机目标提供更好的中心。 This gives a y-offset of about 8.571067812. 这给出了大约8.571067812的y偏移。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM