【角色】
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour {
private Rigidbody Rd;
public int force = 5;
private int score = 0;
public Text text;
public GameObject winText;
void Start () {
Rd = GetComponent<Rigidbody>();
}
void Update () {
float h = Input.GetAxis("Horizontal");
float w = Input.GetAxis("Vertical");
Rd.AddForce(new Vector3(h,0,w)*force);
}
//private void OnCollisionEnter(Collision collision)//获取碰撞到的游戏物体上的Collision组件【碰撞器】
//{
// /* string name = collision.collider.name;*///获取碰撞到的游戏物体名字
// //Debug.Log(name);
// //GameObject obj = GameObject.FindWithTag("pikup");
// if (collision.collider.tag=="pikup") {//判断碰撞到的物体的tag是不是等于pikup,如果是就把他删掉!
// Destroy(collision.collider.gameObject,3f);//删除物体(string,time)
// }
//}
private void OnTriggerEnter(Collider collider)//获取触发的物体【不是碰撞】
{
if (collider.tag == "pikup") {//【同理】
score++;
text.text = score.ToString();
if (score == 16)//当score=11的时
{
winText.SetActive(true);//激活winText【原未激活状态】
}
Destroy(collider.gameObject);
}
}
}
【相机】
using UnityEngine;
using System.Collections;
public class FollowTarget : MonoBehaviour {
public Transform playerTransform;
private Vector3 lala;
void Start () {//只执行一次
lala = transform.position - playerTransform.position;//让相机的坐标减去玩家的坐标等于坐标差(向量)
}
// Update is called once per frame
void Update () {
transform.position = playerTransform.position + lala;//把坐标差加玩家的坐标赋给相机,相机就会保持跟踪
}
![Uploading 1_101583.jpg . . .]
}
【绕某点旋转】
using UnityEngine;
using System.Collections;
public class PickUp : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Rotate(new Vector3 (0,1,0));//物体围绕某个点进行旋转
}
}