先上一下效果图:
(图片是百度随便搜的,简单糊了一下)
- 场景里放置Plane平面 自己可以随便放置,改改颜色,大概加个贴图看成地板就好了;
- 新建一个Cube,也可以随便改改颜色,醒目一点,我是看到个悦目标彩虹条就贴上去了,记得添加Rigibody组件;
- 调解摄像机角度与位置,最幸亏物体后方,而且添加跟随代码:
相机的插值跟随可以参考我之前的帖子:《摄像机的插值跟随》
- 调解窗口2D显示,并在Hierarchy中新建UI→Canvas组件,调解其到符合位置:
在Canvas里右击新建Panel组件,然后在panel属性中得Image组件里得Source Image中选择我们下载的转速表图片,根据需要调解位置、尺寸等;
(注意,这里的图片最好改成JPG格式,也就是在Project中会看到个下拉箭头,这样才可以被panel使用)
5. 在Panel下新建一个空的GameObject组件,并在下方新建Image组件,调解颜色与形状大致成指针状,调解其父子关系的位置,使GameObject可以绕着表盘的中心转动;
6. 为GameObject添加代码:此中ShowSpeed()要为public,方便我们在运动代码中调用;
- using System.Collections;using System.Collections.Generic;using UnityEngine;public class Rotate : MonoBehaviour{ static float minAngle = 0.0f; static float maxAngle = -180f; //0-180代表表回旋转的角度 static Rotate thisSpeed0; // Start is called before the first frame update void Start() { thisSpeed0 = this; } // Update is called once per frame public static void ShowSpeed(float speed, float min, float max) { //将速度与指针转动角度对应起来 float ang = Mathf.Lerp(minAngle, maxAngle, Mathf.InverseLerp(min, max, speed)); //添加旋转 可以在ang后乘以个常数用来调治转动效果 thisSpeed0.transform.eulerAngles = new Vector3(0, 0, ang*10f); }}
复制代码
- 为Cube添加运动代码(需要调用上部的ShowSpeed()功能):
- using System.Collections;using System.Collections.Generic;using UnityEngine;public class move : MonoBehaviour{ public float speed=0; float h = 0; Rigidbody rg; // Start is called before the first frame update void Start() { rg = this.GetComponent(); } // Update is called once per frame void Update() { float v = Input.GetAxis("Vertical")*0.3f;//垂直轴 h = Input.GetAxis("Horizontal");//水平轴 for (int i = 0 ; i < 10; i++) { speed *= v; movetion(); speed++; } movetion(); Rotate.ShowSpeed(rg.velocity.magnitude, 0, 100); } private void movetion () { rg.AddForce(h, 0, 2* speed); }}
复制代码 因为控制代码中使用了“Vertical与Horizontal”,所以可以用上、下、左、右,或W、S、A、D举行控制了;
注意:
Cube中的Rigibody组件,最好限制下Y轴的垂直运动,要否则会不受你控制的。
来源:https://blog.csdn.net/qq_42434073/article/details/111941068
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |