cube克隆加重力
Cube旋转需要旋转类:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Exercise : MonoBehaviour
{
public Texture texture = null;
private GameObject CubeGo;
public void ButtonCubeBorn() //Cube生成
{
CubeGo = GameObject.CreatePrimitive(PrimitiveType.Cube);
CubeGo.transform.position = Vector3.zero;
CubeGo.tag = "Player";
}
public void ButtonColor() //添加颜色
{
if (CubeGo != null)
{
CubeGo.GetComponent<Renderer>().material.color = Color.red;
CubeGo.GetComponent<Renderer>().material.mainTexture = null;
}
}
public void ButtonMaterial() //添加材质
{
if (CubeGo != null)
{
CubeGo.GetComponent<Renderer>().material = null;
CubeGo.GetComponent<Renderer>().material.mainTexture = texture;
}
}
public void ButtonRotate() //旋转Cube
{
if (CubeGo != null)
{
CubeGo.AddComponent<Rotate>();
}
}
public void ButttonClone() //Cube克隆
{
for (int i = 0; i < 10; i++)
{
CubeGo = (GameObject)Instantiate(CubeGo);
CubeGo.transform.position = new Vector3(CubeGo.transform.position.x + 2, CubeGo.transform.position.y, CubeGo.transform.position.z);
}
}
public void ButtonAddRigi() //添加重力
{
GameObject[] goes = GameObject.FindGameObjectsWithTag("Player");
for (int i = 0; i < goes.Length; i++)
{
goes[i].AddComponent<Rigidbody>();
}
}
public void ButtonRigiDistroy() //重力消失
{
if (CubeGo != null)
{
Destroy(CubeGo.GetComponent<Rigidbody>());
CubeGo.transform.position = Vector3.zero;
}
}
public void ButtonCubeDistory() //Cube消失
{
if (CubeGo != null)
{
GameObject[] goes = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject item in goes)
{
Destroy(item);
}
}
}
public void ButtonExit() //退出游戏
{
Application.Quit();
}
}
旋转类:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotate : MonoBehaviour //旋转类
{
void Update()
{
this.transform.Rotate(Vector3.up, 1f);
}
}
场景切换
using UnityEngine.SceneManagement;
public void LoadScene()
{
SceneManager.LoadScene("Scene2");
}
控制摄像机或物体移动
public class Move : MonoBehaviour
{
public float Speed = 3;
void Update()
{
this.transform.Translate(Input.GetAxis("Vertical") * Vector3.forward*Speed * Time.deltaTime);
this.transform.Translate(Input.GetAxis("Horizontal") * Vector3.right*Speed * Time.deltaTime);
}
}
控制物体移动:
CharacterController cc;
void Start()
{
cc = this.GetComponent<CharacterController>();
}
void Update()
{
if (Input.GetKey(KeyCode.W))
{
cc.Move(Vector3.forward * Time.deltaTime*10);
}
if (Input.GetKey(KeyCode.S))
{
cc.Move(Vector3.back * Time.deltaTime * 10);
}
if (Input.GetKey(KeyCode.A))
{
cc.Move(Vector3.left * Time.deltaTime * 10);
}
if (Input.GetKey(KeyCode.D))
{
cc.Move(Vector3.right * Time.deltaTime * 10);
}
}
碰撞掉血
胶囊体:胶囊体挂胶囊 画布挂UI UIplayer挂带脚本的胶囊体对象 墙体标签为wall
public class Capsule : MonoBehaviour
{
public int maxHP=10; //最大血量
public int currentHP; //当前血量
public float Speed = 10;
void Start()
{
currentHP = maxHP; //初始化当前血量为最大
}
void Update() //移动
{
this.transform.Translate(Input.GetAxis("Vertical") * Vector3.forward * Speed * Time.deltaTime);
this.transform.Translate(Input.GetAxis("Horizontal") * Vector3.right * Speed * Time.deltaTime);
}
private void OnCollisionEnter(Collision collision) //碰撞器
{
if (collision.transform.tag=="Wall") //判断碰撞物的标签是wall
{
Wound(); //执行掉血
}
}
public void Wound()
{
if (currentHP > 0)
{
currentHP--; //掉血
}
}
}
slider值类:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UI : MonoBehaviour
{
public Capsule player;
public Slider sliderHP;
void Update()
{
sliderHP.value = player.currentHP;
}
}
射击围墙
预制体Cube和预制体子弹Bullet (刚体)、空对象
public class Shot : MonoBehaviour
{
public float Speed = 3;
public GameObject Bullet;
void Update()
{
if(Input.GetMouseButtonDown(0))
{
GameObject b = GameObject.Instantiate(Bullet, transform.position, transform.rotation);
Rigidbody rib = b.GetComponent<Rigidbody>();
rib.velocity = transform.forward * Speed;
}
}
}
摄像机跟随
public class Camera : MonoBehaviour
{
public Transform follow;
public float distanceAway = 5.0f;
public float distanceUp = 2.0f;
public float smooth = 1.0f;
private Vector3 targePosition;
void LateUpdate()
{
targePosition = follow.position + Vector3.up * distanceUp - follow.forward * distanceAway;
transform.position = Vector3.Lerp(transform.position, targePosition, Time.deltaTime * smooth);
transform.LookAt(follow);
}
}
退出游戏
public class Exit : MonoBehaviour
{
// Start is called before the first frame update
public void OnExitGame()
{
#if UNITY_EDITOR//在编辑器模式退出
UnityEditor.EditorApplication.isPlaying = false;
#else//发布后退出
Application.Quit();
#endif
}
}
小球导航
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
using UnityEngine.AI;
public class Move : MonoBehaviour
{
public Transform target;
NavMeshAgent nav;
void Start()
{
nav = this.gameObject.GetComponent<NavMeshAgent>();
}
void Update()
{
if(nav&&target)
{
nav.SetDestination(target.position);
}
}
}
寻路
寻路:
public GameObject _dest;
UnityEngine.AI.NavMeshAgent _agent;
void Start()
{
_agent = this.GetComponent<UnityEngine.AI.NavMeshAgent>();
}
void Update()
{
_agent.SetDestination(_dest.transform.position);
}
plane开关:
using UnityEngine.AI;
public class 开关 : MonoBehaviour
{
NavMeshObstacle _obs;
void Start()
{
_obs = this.GetComponent<NavMeshObstacle>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
_obs.enabled = false;
this.GetComponent<Renderer>().material.color = Color.green;
}
if (Input.GetMouseButtonUp(0))
{
_obs.enabled = true;
this.GetComponent<Renderer>().material.color = Color.red;
}
}
}
生成cube加射线加移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class 围墙 : MonoBehaviour
{
public GameObject cubeOriginal;
public Image img;
public CharacterController cc;
public Transform m_player;
void Start()
{
cc = this.GetComponent<CharacterController>();
for (int j=0;j<=5;j++)
{
for(int i=0;i<=10;i++)
{
GameObject cubeClone = Instantiate(cubeOriginal);
cubeClone.transform.position = new Vector3(i, j, 0);
}
}
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit))
{
Debug.DrawLine(ray.origin, hit.point);
img.transform.position = Input.mousePosition;
}
if (Input.GetMouseButtonDown(0))
{
GameObject bullet = GameObject.CreatePrimitive(PrimitiveType.Sphere);
bullet.AddComponent<Rigidbody>();
bullet.transform.position = Camera.main.transform.position;
bullet.GetComponent<Rigidbody>().AddForce(600*(hit.point-bullet.transform.position));
Destroy(bullet,3);
}
if (Input.GetKey(KeyCode.W))
{
cc.Move(Vector3.forward * Time.deltaTime * 10);
}
if (Input.GetKey(KeyCode.S))
{
cc.Move(Vector3.back * Time.deltaTime * 10);
}
if (Input.GetKey(KeyCode.A))
{
cc.Move(Vector3.left * Time.deltaTime * 10);
}
if (Input.GetKey(KeyCode.D))
{
cc.Move(Vector3.right * Time.deltaTime * 10);
}
Debug.DrawRay(m_player.position, m_player.forward, Color.red);
}
public void LoadScene()
{
SceneManager.LoadScene("Scene1");
}
}