三个脚本:
1.给求添加力
2.分数管理器
3.检测是否发生碰撞
-----------------------------------------------
脚本源码
1.给求添加力
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class RoleControl : MonoBehaviour
{// 1.玩家控制一个保龄球,按下空格键,开始发射// 拿到物体GameObject MainRole;Rigidbody Onerigi;bool isDown = true;public float ForceDATA = 100f;void Start(){MainRole = GameObject.Find("MainRole");}void Update(){if (Input.GetKeyDown(KeyCode.Space)&&isDown)//如果用户按下空格键{//开始添加力给MainRoleOnerigi = MainRole.GetComponent<Rigidbody>();Onerigi.AddForce(new Vector3(0, 0, -1)* ForceDATA, ForceMode.Impulse);isDown = false;}//end if }//end update
}//end class
2.分数管理器
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;public class ScoreManager : MonoBehaviour
{ // 如果撞倒花瓶就加分 TMP_Text OneWenBen;GameObject wenben;public static int currentScore = 0;private void Start(){wenben = GameObject.Find("Text (TMP)");OneWenBen = wenben.GetComponent<TMP_Text>();}private void LateUpdate(){Debug.Log("恭喜你!得分了!你的分数是:" + currentScore);OneWenBen.text ="Score:"+ currentScore.ToString();}
}
3.检测是否发生碰撞
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CollisionTest : MonoBehaviour
{// 碰撞检测//private void OnTriggerEnter(Collider other)//{// //发生了碰撞// ScoreManager.currentScore += 1;//通知分数管理器类加分// Debug.Log("开始碰撞");// Debug.Log(other.gameObject.name);//}private void OnCollisionEnter(Collision collision){Debug.Log("开始碰撞");if (collision.collider.gameObject.name!="Plane"&& collision.collider.gameObject.name != "Cube"){ScoreManager.currentScore += 1;//通知分数管理器类加分}Debug.Log(collision.collider.gameObject.name);}}