分析 -- 、
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