简体   繁体   English

如何统一停止跟随 Y 的相机

[英]How to stop camera following Y in unity

I have this code and I can't figure out how to make the camera stop following my player when he jumps, in unity3d我有这个代码,但我不知道如何让相机在他跳跃时停止跟随我的球员,在 unity3d 中

using UnityEngine;使用 UnityEngine; using System.Collections;使用 System.Collections;

public class Camera2DFollow2 : MonoBehaviour {公共类 Camera2DFollow2:MonoBehaviour {

public Transform target;
public float damping = 1;
public float lookAheadFactor = 3;
public float lookAheadReturnSpeed = 0.5f;
public float lookAheadMoveThreshold = 0.1f;

float offsetZ;
Vector3 lastTargetPosition;
Vector3 currentVelocity;
Vector3 lookAheadPos;

// Use this for initialization
void Start () {
    lastTargetPosition = target.position;
    offsetZ = (transform.position - target.position).z;
    transform.parent = null;
}

// Update is called once per frame
void Update () {

    // only update lookahead pos if accelerating or changed direction
    float xMoveDelta = (target.position - lastTargetPosition).x;

    bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;

    if (updateLookAheadTarget) {
        lookAheadPos = lookAheadFactor * Vector3.right * Mathf.Sign(xMoveDelta);
    } else {
        lookAheadPos = Vector3.MoveTowards(lookAheadPos, Vector3.zero, Time.deltaTime * lookAheadReturnSpeed);  
    }

    Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
    Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);

    transform.position = newPos;

    lastTargetPosition = target.position;       
}

} }

Try尝试

Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
newPos.y = transform.position.y;
transform.position = newPos;

or或者

Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
aheadTargetPos.y = transform.position.y;
Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
transform.position = newPos;

You can set a minimum threshold value for the Y value of character.您可以为字符的 Y 值设置一个最小阈值。 When characters jump you can save that transform point y and look for the distance between that point and character position.y If the character exceeds that threshold, than your camera can follow the character.当角色跳跃时,您可以保存该变换点 y 并查找该点与角色位置之间的距离。如果角色超过该阈值,则您的相机可以跟随该角色。 Also, you can consider using Cinemachine.此外,您可以考虑使用 Cinemachine。 It is very powerful.它非常强大。 ( https://unity.com/unity/features/editor/art-and-design/cinemachine ) ( https://unity.com/unity/features/editor/art-and-design/cinemachine )

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

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