第一个工程3-代码解释一

本文详细解读了Unity游戏中StickController.cs脚本,涉及球杆旋转、击球方向控制、力度条操作以及重置机制。通过Input.GetAxis调整击球方向,使用KeySpace控制力度条,并在击球后进行场景重置与玩家切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在StickController.cs中

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class StickController : MonoBehaviour {
    public GameObject whiteBall;
    public GameObject tableStick;
    public GameObject whiteBallParticleSystem;
    private WhiteBallParticleSystemController whiteBallParticleSystemController;

    private Vector3 whiteBallOffsetTableStick;
    public Vector3 hitVector;
    public GameObject hitPowerGameObject;
    private Slider hitPower;
    public GameObject sceneManagerObject;
    private SceneManager sceneManager;

    private Rigidbody tableStickRigidBody;
    private Rigidbody whiteBallRigidBody;
    private Quaternion oldTableStickRotation;


    //system configure
    private float tableStickRotationSpeed = 50.0f;
    private float hitSpeed = 80.0f;
    private bool startSlider = false; //空格下上控制状态
    private bool leftToRight = true;
    private Vector3 tempHitVector;
    public bool ifHitReady;
    // Use this for initialization
    void Start () {
        whiteBallOffsetTableStick = tableStick.transform.position - whiteBall.transform.position;  //杆坐标-白球坐标作为两者间的初始偏移,复位用,这里的复位做的比较简单
//        Vector3 temp1 = tableStick.transform.TransformPoint (Vector3.forward);
//        Vector3 temp2 = tableStick.transform.InverseTransformVector(whiteBallOffsetTableStick);
        oldTableStickRotation = tableStick.transform.rotation;  //存储初始球杆的朝向,这是为了基本复位用的
        tableStickRigidBody = tableStick.GetComponent<Rigidbody> ();
        whiteBallRigidBody = whiteBall.GetComponent<Rigidbody> ();
        hitPower = hitPowerGameObject.GetComponent<Slider> ();
        sceneManager = sceneManagerObject.GetComponent<SceneManager> ();
        whiteBallParticleSystemController = whiteBallParticleSystem.GetComponent<WhiteBallParticleSystemController> ();

        ifHitReady = false; //是否开始击球的标志,false表示未开始击球
    }
    
    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown (KeyCode.H)) {
            ResetTableStickAndRelated (); //按h停止白球,杆复位
        }

        float rotation = Input.GetAxis ("Horizontal") * tableStickRotationSpeed * Time.deltaTime;
        tableStick.transform.RotateAround (whiteBall.transform.position, Vector3.up, -20 * Time.deltaTime * rotation); //这两句根据方向键使杆围绕白球转,就是调整击球方向


        if (Input.GetKeyDown (KeyCode.Space)) {
            startSlider = true;  //按下空格键, startSlider = true力度条变为运动状态
        }
        if (startSlider == true) {
            if (leftToRight) {
                hitPower.value++;
                if (hitPower.value > hitPower.maxValue - 1)
                    leftToRight = false;
            } else {
                hitPower.value--;
                if (hitPower.value < hitPower.minValue + 1)
                    leftToRight = true;
            }
        } //startSlider == true时,力度条根据帧往复运动
        if (Input.GetKeyUp (KeyCode.Space)) {
            startSlider = false; //抬起空格,startSlider = false停止力度条
            ifHitReady = true; //击球该是
            hitVector = (whiteBall.transform.position - tableStick.transform.position).normalized; //计算击球方向
        
            tableStickRigidBody.AddForce (hitVector * hitSpeed, ForceMode.Force); //对杆施加里,这里杆只是个运动表示,只要最后碰到白球即可,所以力度是固定的,演示用
        }
    }

    public void ResetTableStickAndRelated()  //重置必要内容,显示杆,开始下一杆击球
    {
        if (sceneManager.resetWhiteBall == true)
            sceneManager.ReSetWhiteBall(); //白球需要重置,就将其重置
        if (sceneManager.resetBlackBall == true)
            sceneManager.ReSetBlackBall(); //黑球需要重置,就将其重置
       
        if (whiteBall.activeSelf == true) {
            whiteBallRigidBody.velocity = Vector3.zero;
            whiteBallRigidBody.angularVelocity = Vector3.zero;
        } //白球没消失,将其停止,这是点H强制停止白球的一个遗留功能

        if (tableStick.activeSelf == false) {
            tableStick.SetActive (true);
            whiteBallParticleSystem.SetActive (true);
        } //重新显示杆和击球线,由于击球后我们会让杆消失。
        tableStickRigidBody.velocity = Vector3.zero;
        tableStick.transform.rotation = oldTableStickRotation;
        tableStick.transform.position = whiteBall.transform.position + whiteBallOffsetTableStick; //简单重置杆的位置

        hitPower.value = 0; //重置击球力
        ifHitReady = false; // ifHitReady = false,上一杆停止

        if (sceneManager.CheckIfNoBallIn ())
            SceneManager.changePlayer = true; //检查,没球进换人
        if(!sceneManager.CheckIfMyBallIn())
            SceneManager.changePlayer = true; //检查,如果我没有球进换人

        if (SceneManager.changePlayer == true) {
            sceneManager.ChangePlayer (); //换人动作
        } else
            sceneManager.ClearBallInThisTure (); //不换人,清空本轮进球列表

        //把球杆转向该打的最小球的方向
        whiteBallParticleSystemController.CurrentPlayerNextBallDirection(whiteBallOffsetTableStick); //把球杆转向该打的最小球的方向,具体看该代码
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值