Unity人物前进的方向和相机朝向一致

2023-10-27

鼠标点击滑动移动相机 代码

using UnityEngine;
using System.Collections;

/* 
 * This is an improved orbit script based on the MouseOrbitImproved script found
 * on the unity community wiki. It should run smoother then the original version
 * 
 * */
[AddComponentMenu("Camera-Control/Mouse drag Orbit with zoom")]
public class DragOrbitImproved : MonoBehaviour
{
	public Transform target;
	public float distance = 500.0f;
	public float xSpeed = 0.1f;
	public float ySpeed = 10.0f;
	
	public float yMinLimit = -20f;
	public float yMaxLimit = 80f;
	
	public float distanceMin = 200f;
	public float distanceMax = 500f;
	
	public float smoothTime = 2f;

	public float zoomSpeed = 100.0f;
	
	float rotationYAxis = 0.0f;
	float rotationXAxis = 0.0f;
	
	float velocityX = 0.0f;
	float velocityY = 0.0f;

	public bool detectColliders = false;
	
	// Use this for initialization
	void Start()
	{
		Vector3 angles = transform.eulerAngles;
		rotationYAxis = angles.y;
		rotationXAxis = angles.x;
		
		// Make the rigid body not change rotation
		if (GetComponent<Rigidbody>())
		{
			GetComponent<Rigidbody>().freezeRotation = true;
		}
	}

	// Late update happens after the normal update, and is used to make sure other 
	// update events have happened first
	void LateUpdate()
	{
		if (target)
		{
			if (Input.GetMouseButton(0))
			{
				velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
				velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
			}
			
			rotationYAxis += velocityX;
			rotationXAxis -= velocityY;
			
			rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
			
			//Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
			Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
			Quaternion rotation = toRotation;
			
			distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * zoomSpeed, distanceMin, distanceMax);


			if (detectColliders) {
				RaycastHit hit;
				if (Physics.Linecast(target.position, transform.position, out hit))
				{
					distanceMin = hit.distance;
				}
			}
			Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
			Vector3 position = rotation * negDistance + target.position;
			
			transform.rotation = rotation;
			transform.position = position;
			
			velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
			velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
		}
		
	}

	// Clamp angle make sure the orbit does not flip the object upside down
	public static float ClampAngle(float angle, float min, float max)
	{
		if (angle < -360F)
			angle += 360F;
		if (angle > 360F)
			angle -= 360F;
		return Mathf.Clamp(angle, min, max);
	}
}

角色代码

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

public class Foxmove1 : MonoBehaviour
{

    public Camera camera; 
    Rigidbody rigidbody;
    public float speed;

    float cameraRotate; //摄像机角度

 
 

    

    void Start()
    {
            rigidbody = this.GetComponent<Rigidbody>();
    
    }

    void Update()
    {

        //记录摄像机角度 并把角度转换为弧度
        cameraRotate = camera.transform.eulerAngles.y/180*Mathf.PI;

        //获取人物当前的运动状态 方向 速度
        Vector3 v = rigidbody.velocity;

        //wasd输入
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");
        if (Mathf.Abs(horizontalInput)>0.05f || Mathf.Abs(verticalInput) >0.05f) //轻敲不作数
        {
            //修改人物运动状态
            float sr = Mathf.Sin(cameraRotate); 
            float cr = Mathf.Cos(cameraRotate); 
             
            
            rigidbody.velocity = new Vector3((verticalInput * sr  + horizontalInput*cr) * speed ,  v.y, (verticalInput * cr-horizontalInput*sr) *speed  );
           //改变人物面朝方向
            transform.rotation = Quaternion.LookRotation(new Vector3((verticalInput * sr + horizontalInput * cr) * speed, 0, (verticalInput * cr - horizontalInput * sr) * speed));
        }
    }
}


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Unity人物前进的方向和相机朝向一致 的相关文章

随机推荐

  • protobuf与json互相转换

    Java http code google com p protobuf java format maven
  • vue + springboot poi 实现excel模板导出 完整代码

    有导入导出功能的时候 避免用户导入数据有误 提供了excel模板下载 用户直接在系统导出的excel模板中填写数据 再导入该excel 例如下图excel 提供前后台所有导出代码 3 代码 3 1 前台 1 按钮
  • Python 循环所有文件夹(含子文件夹),读取指定格式文件,另存为其他格式文件...

    循环所有文件夹 含子文件夹 读取指定格式文件 另存为其他格式文件 与原有文件在同一级目录 并删除原有文件 usr bin python coding utf 8 遍历所有文件夹 将指定格式文件 批量另存为其他文件 或其他格式 import
  • 【OpenCV学习笔记】【函数学习】十四(cvSeq的用法说明(功能很多,按照需求使用))

    OpenCV CvSeq 结构 一直困惑于CvSeq到底是个什么样的东西 因为曾经拿到别人写的一个函数库 其返回值是一个CvSeq指针 我的任务是遍历所有的Sequence 然后删除其中不符合要求的Sequence 由于没有文档 我当时并不
  • 优雅的获取文件及文件夹

    string filepath D WEB var rel Directory GetFiles filepath SearchOption AllDirectories ToList foreach var file in rel Con
  • Java垃圾回收机制

    Java垃圾回收机制 Java垃圾回收机制是指一种自动化的内存管理方式 Java程序员无需手动管理内存 而是由JVM Java虚拟机 自动进行垃圾回收 下面是简要的Java垃圾回收机制 垃圾收集器 JVM中垃圾回收器 Garbage Col
  • 外包征途-甲方、乙方、外包

    甲方 乙方 外包 在IT这行 我们经常都会听到这几个词 那这几个词到底是什么意思 我们先看看官方的解释 甲方 一般是指提出目标的一方 在合同拟定过程中主要是提出要实现什么目标 是合同的主导方 甲方是合同中双方平等主体的代称 也是为了方便在下
  • Meta算力争夺演变成团队动荡!LLaMA、LLaMA2、OPT团队成员多位离职

    据TheInformation报道 原参与Llama项目的团队成员有多位已经辞职 原因是Meta内部的OPT研究团队与Llama团队之间发生了一场关于计算资源的内部斗争 看来不管是谷歌 微软 OpenAI还是Meta 人才流失都是一个避不开
  • 【满分】【华为OD机试真题2023 JS】组装新的数组

    华为OD机试真题 2023年度机试题库全覆盖 刷题指南点这里 组装新的数组 知识点回溯数组 时间限制 1s 空间限制 256MB 限定语言 不限 题目描述 给你一个整数M和数组N N中的元素为连续整数 要求根据N中的元素组装成新的数组R 组
  • iOS 299美元企业账号申请流程及注意事项

    iOS开发者众多 但并不是所有的开发者都对账号申请 证书配置这些问题都清楚 毕竟不是所有开发者都能够经历这个环节 多数情况下是进公司之前这些东西都已经有了 作为一个合格的iOS开发者 我们必须要了解苹果的三种开发者账号 下图对三者进行了比较
  • C# 中的委托和事件(详解)

    C 中的委托和事件 委托和事件在 NET Framework 中的应用非常广泛 然而 较好地理解委托和事件对很多接触 C 时间不长的人来说并不容易 它们就像是一道槛儿 过了这个槛的人 觉得真是太容易了 而没有过去的人每次见到委托和事件就觉得
  • opencv图像处理及视频处理基本操作

    计算机眼中的图像由一个个像素组成 每个像素点的值在0 255之间 代表像素点的亮度 0为最暗 255为最亮 通常彩色图为三通道 灰度图 黑白图 为单通道 彩色图像包括三个颜色通道 B G R 分别表示蓝 绿 红 目录 1 图像的表示 2 图
  • html超链接打开共享文件夹,访问共享文件夹的方法

    在局域网 我们经常会用到共享文件 这样在多人传输文件跟共享资料上就会又方便又快捷啦 在这里教大家怎样建立跟访问共享文件夹 打开控制面板 找到防火墙 点击打开防火墙 弹出防火墙设置面板 我们选择关闭防火墙 虽然写不推荐 但我们可以无视它 然后
  • 抖音自动生成文字_文字动画怎么制作?这里教你一键制作抖音爆款文字视频

    现在很多人都会在闲暇时间打开抖音刷刷视频 经常会看到很多文字视频特别有趣 配上动感的节奏 文字立刻鲜活起来 如何才能制作出这样的文字视频呢 今天给大家介绍一种全网最简单的抖音文字视频制作方法 不需要会使用AE 甚至也不需要你打字 直接语音识
  • 关于python基础,90%的人不知道这些。但你一定得清楚。

    经过前几次的学习我们已经安装好Python解释器 搭建好顺手的IDE环境 那么接下来 我们就正式的开始一些列Python知识的学习 代码敲起来 一 字面量 字面量是以变量或常量给出的原始数据 在Python中 有多种类型的字面量 如数字字面
  • linux中断实验

    文章目录 一 linux中断简介 1 linux中断API函数 1 中断号 2 request irq函数 3 free irq 4 中断处理函数 5 中断使能与禁止函数 2 上半部与下半部 1 软中断 2 tasklet 3 工作队列 3
  • jboss源码中片段分析

    package com test import java security AccessController import java security PrivilegedAction import java util ArrayList
  • QRegExp

    d 非负整数 正整数 0 0 9 1 9 0 9 正整数 d 0 非正整数 负整数 0 0 9 1 9 0 9 负整数 d 整数 d d 非负浮点数 正浮点数 0 0 9 0 9 1 9 0 9 0 9 1 9 0 9 0 9 0 9 1
  • post使用方法以及有道API

    import requests import json headers User Agent Mozilla 5 0 Windows NT 10 0 Win64 x64 AppleWebKit 537 36 KHTML like Gecko
  • Unity人物前进的方向和相机朝向一致

    鼠标点击滑动移动相机 代码 using UnityEngine using System Collections This is an improved orbit script based on the MouseOrbitImprove