【unity】几个常用脚本

it2022-05-08  8

1.让人物自动到达点击位置

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class Hero : MonoBehaviour { public NavMeshAgent agent; public Animator anim; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if(Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; //碰撞信息 if(Physics.Raycast(ray, out hit)) { agent.SetDestination(hit.point); } } anim.SetFloat("speed", agent.velocity.magnitude); } }

2.让摄像机跟随绑定人物移动

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Follow : MonoBehaviour { public Transform hero; //跟随的人物 private Vector3 offset; //偏移量 // Start is called before the first frame update void Start() { offset = transform.position - hero.position; } // Update is called once per frame void Update() { transform.position = offset + hero.position; } }

3.让昼夜交替

平行光的自转可以影响太阳光照所以给它加一个脚本可以实现昼夜交替

using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Day_Night_Control : MonoBehaviour { public float rotateSpeed = 10; //旋转角度 // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime, Space.Self); } }

 


最新回复(0)