1.球体的移动和碰撞检测
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Playshop : MonoBehaviour
{
private Rigidbody shopt;
public GameObject txt_out;
public float speet = 8;
public Text txt;
//private Rigidbody y;
private int score = 0;
//private Rigidbody y;
//public GameObject shopt;//拖拽获取组件
// Start is called before the first frame update
void Start()
{
shopt = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
//1
//GameObject y = GameObject.FindWithTag("play");//获取游戏物体
//Rigidbody c = y.GetComponent<Rigidbody>();//获取刚体组件
//2
//Rigidbody y = GetComponent<Rigidbody>();//获取当前游戏物体的刚体组件
//3
//y = shopt.GetComponent<Rigidbody>();//获取拖拽游戏物体的刚体组件
//移动
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
shopt.AddForce(new Vector3(h, 0, v)*speet);//刚体移动函数AddForce()
}
//碰撞检测
//is Trigger可以关闭或默认
void OnCollisionEnter(Collision collision)
{
//发生实际的物理效果
//string name = collision.collider.name;
//collision.collider获取游戏物体cube身上的collider组件
//print(name);
if (collision.collider.tag == "picup") {
Destroy(collision.collider.gameObject);//销毁方法
};
}
//触碰检测
//is Trigger要开启
void OnTriggerEnter(Collider collider)
{
Debug.Log(123);
if (collider.tag == "picup")
{
score++;
txt.text = score.ToString();//将number类型转为字符串类型
if (score == 19) {
txt_out.SetActive(true);//显示与隐藏
}
Destroy(collider.gameObject);//销毁
};
}
}
2.方块的自动旋转
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pickup : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Rotate(new Vector3(0,1,0));//围绕自身Y轴旋转
}
}
3.相机的跟随
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Play_camera : MonoBehaviour
{
public Transform playerTransform;
public Vector3 offset;
// Start is called before the first frame update
void Start()
{
//相机的位置
offset = transform.position - playerTransform.position;
}
// Update is called once per frame
void Update()
{
transform.position = playerTransform.position + offset;
}
}