小游戏 搭建 投篮 --项目

本文介绍了使用Unity创建投篮小游戏的步骤,包括篮球的创建和状态管理、坐标与方向设定、滑动时间检测及物理力应用、以及碰撞检测和处理。通过该项目,可以深入理解Unity中的物理系统和碰撞检测技术。
摘要由CSDN通过智能技术生成

分析  --  、
  1.  篮球的创建和状态
  2.  篮球的坐标和方向确定
  4.  滑动时间检测,和物理力的分配
  5 .  碰撞检测 ,碰撞捆绑

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shoter2 : MonoBehaviour
{
    public enum ShotState
    {
        Charging,
        Ready,
        DirextionAndPower
    }
    private ShotState m_ShotState;

    public Transform ShotPoint;
    public GameObject ballPrefab;
    public GameObject ObjBall;
    private Rigidbody ballRigi;

    //==
    private float offsetZShotPoint = 1.0f;

    private void Start()
    {
        Todpos.x = -1;
        m_ShotState = ShotState.Charging;
    }
    public void Update()
    {
        if (m_ShotState == ShotState.Charging)
        {
            EquipmentBell();
            // OnTriigleHit();
        }
        else if (m_ShotState == ShotState.Ready)
        {
            OnTriigleHit();
        }
        else if (m_ShotState == ShotState.DirextionAndPower)
        {
            if (Input.GetMouseButtonUp(0))
            {
                if (ObjBall != null)
                {
                  ShotBall();
                  m_ShotState = ShotState.Charging;
                  ObjBall = null;
                }
            }
        }
    }

    public void EquipmentBell()
    {
        if (ObjBall == null)
        {
            ObjBall = Instantiate<GameObject>(ballPrefab);
            ObjBall.AddComponent<Rigidbody>();
            ObjBall.AddComponent<ShotBall>();
            ballRigi = ObjBall.GetComponent<Rigidbody>();
            Vector3 Startpos = ShotPoint.transform.localPosition;
            Startpos.z -= offsetZShotPoint;
            ObjBall.transform.position = ShotPoint.transform.TransformPoint(Startpos);
            ObjBall.transform.eulerAngles = ShotPoint.transform.eulerAngles;
            ballRigi.AddForce(ShotPoint.transform.TransformDirection(new Vector3(0, 0, 2f)), ForceMode.Impulse);
        }
        else
        {
            float dir = Vector3.Distance(ShotPoint.position, ObjBall.transform.position);
            if (dir <= 0.05f)
            {
                m_ShotState = ShotState.Ready;
                ballRigi.transform.position = ShotPoint.position;
            }
        }
    }


    Vector3 Todpos;
    float shotPower;
    float m_StartTime;
    //--
    public float Timemin = 0.2f;
    public float Timemax = 0.55f;
    public float shotPowerMax = 10f;
    public float shotPowerMin = 3f;
    public float OffsetY = 100;
    public void OnTriigleHit()
    {
        if (Todpos.x < 0)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit raycastHit;
                if (Physics.Raycast(ray, out raycastHit))
                {
                    ShotBall shotBall = raycastHit.collider.GetComponent<ShotBall>();
                    if (shotBall != null && !shotBall.isActive)
                    {
                        shotBall.ChangeActive();
                        shotPower = 0;
                        Todpos = Input.mousePosition;
                    }
                }
            }
        }
        else
            {
                if (Todpos.x != Input.mousePosition.x|| Todpos.y!=Input.mousePosition.y)
                {
                    Todpos.x = -1;
                    m_StartTime = Time.time;
                    m_ShotState = ShotState.DirextionAndPower;
                }
            }
    }

    public void ShotBall()
    {
        float ExitTime = Time.time - m_StartTime;
        if (ExitTime > Timemax)
            shotPower = shotPowerMin;
        else if (ExitTime < Timemin)
            shotPower = shotPowerMax;
        else
        {
            /*
            float powerMax = shotPowerMax * 10000f;
            float powerMin = shotPowerMin * 10000f;
            float time = ExitTime * 10000f;
            */
            float ro = (ExitTime-Timemin)/(Timemax-Timemin);
            Debug.LogError(ro+ "     ro");
            shotPower = shotPowerMax - (shotPowerMax - shotPowerMin) * ro;
        }

        Vector3 vector3 = Input.mousePosition;
        vector3.z = Camera.main.WorldToScreenPoint(ShotPoint.position).z+12;  //增加深度   获取里力的方向
        Vector3 WroidPos = Camera.main.ScreenToWorldPoint(vector3);
        WroidPos.y += OffsetY / shotPower;
        //获取方向
        Vector3 vec = (WroidPos - ShotPoint.position).normalized;
        Debug.LogError(shotPower);
        ballRigi.velocity = vec * shotPower;
        ballRigi.AddTorque(-ShotPoint.transform.right*30,ForceMode.Force);
     
    }

   void FixedUpdate()
    {
        if (m_ShotState != ShotState.Charging)
            ballRigi.velocity = Vector3.zero;
    }


}

项目连接    unity 2018-3-6

https://download.csdn.net/download/xuxian_sheng/11050072

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值