【学习日志】2022.09.19 Unity摄像机跟随(插值去抖动)、移动&旋转式移动

Unity摄像机跟随 

cb240b891f534bcdbafba634cc981f3e.png

 普通跟随

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

public class FollowCam : MonoBehaviour
{
    public Transform target;
    Vector3 offset;

    // Start is called before the first frame update
    void Start()
    {
        offset = transform.position - target.position;
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 newPos = target.position + offset;
        transform.position = newPos;
    }
}

插值去抖跟随

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

public class FollowCam : MonoBehaviour
{
    public Transform target;
    Vector3 offset;

    // Start is called before the first frame update
    void Start()
    {
        offset = transform.position - target.position;
    }

    // Update is called once per frame
    void Update()
    {        
        Vector3 newPos = target.position + offset;
        //Lerp插值,(位置1,位置2,0-1之间的数)
        Vector3 pos = Vector3.Lerp(transform.position, newPos, 0.5f);

        transform.position = pos;
    }
}

LookAt 聚光灯跟随(相机位置不动)

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

public class FollowCam : MonoBehaviour
{
    public Transform target;
    Vector3 offset;

    // Start is called before the first frame update
    void Start()
    {
        offset = transform.position - target.position;
    }

    // Update is called once per frame
    void Update()
    {
        //转向目标点
        transform.LookAt(target);
    }
}

普通移动

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

public class Move : MonoBehaviour
{
    public float speed = 3.0f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        Vector3 input = new Vector3(h, 0, v);
        if (input.magnitude > 1)
        {
            input = input.normalized;
        }

        //考虑时间,抵消帧率变化的影响
        transform.position += input * speed * Time.deltaTime;           //世界坐标系
        transform.Translate(input * speed * Time.deltaTime,Space.World);//世界坐标系
        transform.Translate(input * speed * Time.deltaTime);            //自身坐标系
        transform.Translate(input * speed * Time.deltaTime,Space.Self); //自身坐标系
    }
}

旋转式移动

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

public class RotateMove : MonoBehaviour
{
    public float speed = 3.0f;
    public float rotateSpeed = 3.0f;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        //h控制旋转
        transform.Rotate(new Vector3(0, h * rotateSpeed * Time.deltaTime, 0));

        //v控制前进后退
        transform.Translate(new Vector3(0, 0, v) * speed * Time.deltaTime);


    }
}

 

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值