using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camerafollow : MonoBehaviour {
public GameObject targetobject;
private float distance;
// Use this for initialization
void Start () {
distance = transform.position.x - targetobject.transform.position.x;
}
// Update is called once per frame
void Update () {
float targetobjectx = targetobject.transform.position.x;
Vector3 newcamerapostion = transform.position;
newcamerapostion.x = targetobjectx+distance ;
transform.position = newcamerapostion;
}
}
using System.Collections.Generic;
using UnityEngine;
public class camerafollow : MonoBehaviour {
public GameObject targetobject;
private float distance;
// Use this for initialization
void Start () {
distance = transform.position.x - targetobject.transform.position.x;
}
// Update is called once per frame
void Update () {
float targetobjectx = targetobject.transform.position.x;
Vector3 newcamerapostion = transform.position;
newcamerapostion.x = targetobjectx+distance ;
transform.position = newcamerapostion;
}
}
本文介绍了一个简单的Unity相机跟随脚本实现方法。通过设定目标对象和距离参数,在游戏运行过程中,使摄像机始终保持与目标对象固定水平距离,实现平滑跟随效果。此脚本适用于2D或3D游戏开发中需要摄像机跟随玩家或其他对象移动的场景。
2369

被折叠的 条评论
为什么被折叠?



