Project V0.1 Record
coconutnut

做了个初版试试水~😃

功能

点击寻路

靠近显示介绍

鼠标滚动前后平移

键盘前后左右平移

鼠标拖拽旋转视角

项目结构

代码

PlayerController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
[SerializeField, Range(0f,500f)]
private float mouseLookSensitivity = 200f;

[SerializeField, Range(0f,100f)]
private float mouseMoveSensitivity = 10f;

[SerializeField, Range(0f,10f)]
private float keyMoveSensitivity = 5f;

[SerializeField]
private Transform cameraTransform = null;

private float cameraRotation = 0f;
private int mouseDownCount = 0;

UnityEngine.AI.NavMeshAgent agent;

GameObject clickEffect;

void Start()
{
agent = GetComponent<UnityEngine.AI.NavMeshAgent>();

clickEffect = Resources.Load<GameObject>("Prefabs/ClickEffect");
}

void Update()
{
if(Input.GetMouseButtonUp(0)){
// if is click
if(mouseDownCount < 2){
// click to navigate
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, Mathf.Infinity)){
// move when hit floor
if(hit.transform.tag == "Floor"){
agent.SetDestination(hit.point);

// click effect
GameObject clickEffectObject = Instantiate(clickEffect);
clickEffectObject.transform.position = hit.point;
Destroy(clickEffectObject, 1);
}
}
}
mouseDownCount = 0;

}else if(Input.GetMouseButton(0)){
// stop navigation
agent.ResetPath();

// get mouse position
float mouseX = Input.GetAxis("Mouse X") * mouseLookSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseLookSensitivity * Time.deltaTime;

// rotate camera up and down
cameraRotation -= mouseY;
cameraRotation = Mathf.Clamp(cameraRotation, -30f, 30f);
cameraTransform.localRotation = Quaternion.Euler(cameraRotation, 0f, 0f);

// rotate player left and right
transform.Rotate(Vector3.up * (mouseX));

mouseDownCount++;

}else if(Input.GetAxis("Mouse ScrollWheel")!=0 || Input.GetAxis("Horizontal")!=0 || Input.GetAxis("Vertical")!=0){
// stop navigation
agent.ResetPath();

// mouse scroll input
float mouseV = Input.GetAxis("Mouse ScrollWheel");
// keyborad input
float keyV = Input.GetAxis("Vertical");
float keyH = Input.GetAxis("Horizontal");

Vector3 move = transform.forward * (mouseV * mouseMoveSensitivity + keyV * keyMoveSensitivity)
+ transform.right * keyH * keyMoveSensitivity;

agent.Move(move * Time.deltaTime);
}
}
}

ArtTextController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ArtTextController : MonoBehaviour
{
[SerializeField]
private string title;

[SerializeField]
private string description;

[SerializeField, Range(0f,10f)]
private float displayTextDistance = 5f;

private Transform playerTransform;

private Text displayArtTitle;
private Text displayArtDescription;

private float dist;

private bool check;

void Start()
{
// get player transform
playerTransform = GameObject.FindWithTag("Player").GetComponent<Transform>();

// get UI text
displayArtTitle = GameObject.FindWithTag("ArtTitle").GetComponent<Text>();
displayArtDescription = GameObject.FindWithTag("ArtDescription").GetComponent<Text>();

// set default text
displayArtTitle.text = "";
displayArtDescription.text = "";

check = false;
}

// Update is called once per frame
void Update()
{
if(playerTransform)
{
// distance between art and player
dist = Vector3.Distance(transform.position, playerTransform.position);
if (dist < displayTextDistance)
{
displayArtTitle.text = title;
displayArtDescription.text = description;
check = true;
}
// turn off display
if (dist > displayTextDistance && check == true)
{
Start();
}
}
}
}

构建测试

MacOS 没问题

Android 试了一下,场景没问题,操作混乱

​ 安卓导出参考AR Foundation笔记01 安卓Setup

​ 目前的操作方式针对电脑,手机端以后再看要不要做

WebGL 整了半天

​ 参考WebGL Export in Unity 2020

​ 成功发布到itch.io,但是靠近显示介绍没有了?

​ 本地Firefox打开,也没有

WebGL Debug

Unity里跑没问题

Debug.Log人物和画的距离,可以显示,所以可能是

​ UI显示出了问题

​ 文字没有赋值成功

​ 位置偏了

给Text设了默认值,再试

默认值显示了

靠近没有了

所以应该是位置不对

不过后面准备改显示方法,暂时MARK,以后放UI的时候也有可能碰到这个问题