相机跟随物体移动的两种方法
第一种方法
将Mian Camera 拖入物体的层级 成为它的子级 但这个方法只适用与物体不旋转 且物体移动的代码是transform tansalate
第二种方法
调整摄影机至物体与摄影机的距离合适,在摄影机那创建脚本
public Vector3 dis;
//dis存储距离(三维矢量)
public GameObject ball;
//ball存储要跟随的游戏对象
// Start is called before the first frame update
void Start()
{
dis = ball.transform.position - transform.position;
//获取自身与小球的距离并赋予给dis
}
// Update is called once per frame
void Update()
{
transform.position = ball.transform.position - dis;
//自身的坐标等于小球的坐标减去开始的距离
}
-
UI
##文本创建 可修改文本字体大小颜色位置(按住alt进行)可在game视图和scene视图查看
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class qiu : MonoBehaviour
{
public float MoveSpeed;
public Rigidbody rg;
private int score;
public Text fenshu;
public Text ying;
public Button tc;
public Button cx;
// Start is called before the first frame update
void Start()
{
MoveSpeed = 100.0f;
rg = GetComponent<Rigidbody>();
ying.gameObject.SetActive(false);
tc.gameObject.SetActive(false);
cx.gameObject.SetActive(false);
score = 0;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.W))
{
rg.AddForce(Vector3.forward * Time.deltaTime * MoveSpeed);
}
if (Input.GetKey(KeyCode.A))
{
rg.AddForce(Vector3.left * Time.deltaTime * MoveSpeed);
}
if (Input.GetKey(KeyCode.S))
{
rg.AddForce(Vector3.back * Time.deltaTime * MoveSpeed);
}
if (Input.GetKey(KeyCode.D))
{
rg.AddForce(Vector3.right * Time.deltaTime * MoveSpeed);
}
}
private void OnTriggerEnter(Collider other)
{
Destroy(other.gameObject);
fenshu.text = "分数:" + ++score;
if (score>=5)
{
ying.gameObject.SetActive(true);
cx.gameObject.SetActive(true);
tc.gameObject.SetActive(true);
}
}
> ##按钮创建 重新开始
using System.Collections;
using System.Collections.Generic;
usingUnityEngine;
using UnityEditor.SceneManagement;
usingUnityEngine.SceneManagement;
public class anjian1 : MonoBehaviour {
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void Click()
{
EditorSceneManager.LoadScene("SampleScene");//在unity应用中退出
// SceneManager.LoadScene("SampleScene");//在程序运行中退出
}
}
退出按钮
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class anjian : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void Click()
{
EditorApplication.isPlaying = false;
//在unity界面退出
// Application.Quit();
//程序运用退出
}
}