相机跟随物体移动,创建ui 创建按钮

相机跟随物体移动的两种方法
第一种方法
将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;
        //自身的坐标等于小球的坐标减去开始的距离
    }
  1. 在跟随摄影机的属性面板更改

    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();  
        //程序运用退出
    }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值