Unity3D 相机跟随主角移动

这里给主相机绑定一个脚本。

脚本写为:

using UnityEngine;
using System.Collections;

public class camerafollow : MonoBehaviour {
    //主摄像机跟随主角一起移动
    public float xMargin = 1f;
    public float yMargin = 1f;
    public float xSmooth = 8f;
    public float ySmooth = 8f;
    public Vector2 maxXandY;
    public Vector2 minXandY;
	// Use this for initialization
    private Transform player;
	void Start () {//这里获得是绑定的主角,需要一起跟随移动,就要获得主角的属性
        player = GameObject.FindGameObjectWithTag("pk_0").transform;
        maxXandY.x = 10; maxXandY.y = 10;
	}
    bool checkxmargin() {
        return Mathf.Abs(transform.position.x - player.position.x) > xMargin;
    }
    bool checkymargin() {
        return Mathf.Abs(transform.position.y - player.position.y) > yMargin;
    }
	// Update is called once per frame
	void Update () {
        Trackplayer();
	}
    //跟踪主角
    void Trackplayer() {
        float targetx = transform.position.x;
        float targety = transform.position.y;
        if (checkxmargin())
        {
            targetx = Mathf.Lerp(transform.position.x, player.position.x, xSmooth * Time.deltaTime);

        }
        if (checkymargin())
        {
            targety = Mathf.Lerp(transform.position.y, player.position.y, xSmooth * Time.deltaTime);
        }
        targetx = Mathf.Clamp(targetx, minXandY.x, maxXandY.y);
        targety = Mathf.Clamp(targety, minXandY.y, maxXandY.y);
        transform.position = new Vector3(targetx, targety,transform.position.z);
    }
}

  效果图:

时间: 2024-12-19 11:47:10

Unity3D 相机跟随主角移动的相关文章

Unity3D——相机跟随物体移动

public Transform target; public float moveSmooth=5f; Vector3 offset; void Start () { offset = transform.position - target.position;//获取相对位置 } void Update () { Vector3 targetPostion= offset + target.position; transform.position = Vector3.Lerp(transfor

unity3d简单的相机跟随及视野旋转缩放

1.实现相机跟随主角运动 一种简单的方法是把Camera直接拖到Player下面作为Player的子物体,另一种方法是取得Camera与Player的偏移向量,并据此设置Camera位置,便能实现简单的相机跟随了. 这里我们选取第二种方法,首先给Camera添加一个脚本,取名为FollowPlayer,脚本很简单不做说明了 1 public class FollowPlayer : MonoBehaviour { 2 3 private Transform player; 4 private V

Unity3D 利用FSM设计相机跟随实现

笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,国家专利发明人;已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解>电子工业出版社等. CSDN视频网址:http://edu.csdn.net/lecturer/144 FSM有限状态机前面已经跟读者介绍过,使用Unity3D引擎实现了动作状态以及技能切换,FSM使用的条件是有限个状态切换,我们可以将FSM应用到相机中,很多人会问在相机中如何使用FS

unity3D:游戏分解之角色移动和相机跟随

游戏中,我们经常会有这样的操作,点击场景中某个位置,角色自动移动到那个位置,同时角色一直是朝向那个位置移动的,而且相机也会一直跟着角色移动.有些游戏,鼠标滑动屏幕,相机就会围绕角色旋转. 看似很简单的操作,那么到底是怎么实现的呢? 我们把上述操作分解为以下几个步骤 角色的移动 1. 移动到下一个路点,线性插值.曲线插值 2. 角色朝向,一直面朝下一个路点 相机跟随角色 1. 相机俯视角度,决定相机的高度 2. 相机跟随距离,前向距离或者直线距离(就是三角形的水平边长或者斜边长) 3. 相机一直看

unity 常用的几种相机跟随

固定相机跟随 这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 1 using UnityEngine; 2 using System.Collections; 3 4 public class CameraFlow : MonoBehaviour 5 { 6 public Transform target; 7 private Vector3 offset; 8 // Use this for initialization 9 void Start() 10 {

Unity3D 相机路径设置 iTween &amp; Camera Path Animator

unity3d动画插件iTween-路径动画的制作 [教程] 如何使用Unity制作虚拟导览 这里记录下Camera Path Animator插件的时候,下载完成后,安装.之后在 在属性中添加镜头 project中可以看到添加的五个点,可以微调视角,这里绑定的是Main Camera,也可以控制速度,时间等. Unity3D 相机路径设置 iTween & Camera Path Animator

unity 相机跟随物体(角色)

unity 相机平滑跟随游戏角色 把这个脚本赋给你的摄像机,再把游戏角色赋给character变量,之后就能实现摄像机平滑的跟随player在地球的任一角落了. using UnityEngine; using System.Collections; public class SmoothFollowerObj { private Vector3 targetPosition; private Vector3 position; private Vector3 velocity; private

相机跟随(当移动的物体超过当前移动设备的高度一半的时候,相机跟随)

简单粗暴点,直接甩代码. public Transform target;//需要跟随的物体 float pos; //物体和相机之间的距离 public float smooth = 5.0F; void FixedUpdate() { if (target.position.y > transform.position.y) { pos = target.position.y - this.gameObject.transform.position.y; transform.position

unity相机跟随Player常用方式

固定跟随,无效果(意义不大) 1 public class FollowPlayer : MonoBehaviour 2 { 3 public Transform Player; 4 private Vector3 Offset; 5 6 void Start() 7 { 8 //设置差值 9 Offset= Player.position - transform.position; 10 } 11 12 void Update() 13 { 14 transform.position = Pl