该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
第一个代码
using UnityEngine;
using System.Collections;
public class 蓄力跳跃 : MonoBehaviour
{
bool OnGround; //是否在地面上
float jumpPressure = 0f; //蓄力值
float MinjumpPressure = 3f; //蓄力最小值
public float MaxjumpPressure = 10f; // 蓄力最大值
Rigidbody rbody;
Animator animator;
// Use this for initialization
void Start()
{
OnGround = true; //初始设置在地面上
rbody = GetComponent(); //获取组件
animator = GetComponent();
}
// Update is called once per frame
void Update()
{
if (OnGround) //判断是否在地面上
{
if (Input.GetMouseButton(0)) //hold 按下(住)不放鼠标左键
{
if (jumpPressure < MaxjumpPressure)
{ //如果当前蓄力值小于最大值
jumpPressure += Time.deltaTime * 10f; //则每帧递增当前蓄力值
}
else
{ //达到最大值时,当前蓄力值就等于最大蓄力值
jumpPressure = MaxjumpPressure;
}
print("hold: " + jumpPressure); //测试用,请忽略
}
else //not hold 鼠标松开时
{
if (jumpPressure > 0f)
{ //如果是轻轻按下就松开则把最小蓄力值赋值给当前蓄力值
//如果是按住不松则把上面递增的值传下来
jumpPressure += MinjumpPressure;
//给一个向上速度
rbody.velocity = new Vector3(0f, 0f, jumpPressure);
jumpPressure = 0f; //升空以后把蓄力值重设为0
// OnGround = false; //在地面上设为否 (如果有跳跃,需要使用)
}
}
}
}
/* void OnCollisionEnter(Collision other)
{
//检测是否碰撞到地面
if (other.gameObject.tag == "Ground")
{
OnGround = true;
}
}*/
}
第二个代码
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour
{
//在场景中鼠标点击地面后,角色可以移动到目标位置
private Vector3 target;
private bool isOver = true;
public float speed;
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
print("MouseDown");
//1. 获取鼠标点击位置
//创建射线;从摄像机发射一条经过鼠标当前位置的射线
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//发射射线
RaycastHit hitInfo = new RaycastHit();
if (Physics.Raycast(ray, out hitInfo))
{
//获取碰撞点的位置
if (hitInfo.collider.name == "Plane")
{
target = hitInfo.point;
target.y += 0.5f;
isOver = false;
}
}
//RaycastHit[] hitAll = Physics.RaycastAll(ray, 1000);
//foreach(RaycastHit hitInfo in hitAll)
//{
// print(hitInfo.collider.name);
// if (hitInfo.collider.name == "Plane")
// {
// target = hitInfo.point;
// target.y = 0.5f;
// isOver = false;
// }
//}
}
//2. 让角色移动到目标位置
MoveTo(target);
}
//让角色移动到目标位置
private void MoveTo(Vector3 tar)
{
if (!isOver)
{
Vector3 offSet = tar - transform.position;
transform.position += offSet.normalized * speed * Time.deltaTime;
if (Vector3.Distance(tar, transform.position) < 0.5f)
{
isOver = true;
transform.position = tar;
}
}
}
}
怎样将俩代码组合起来实现小球向鼠标蓄力方向发射且有惯性!!!!谢谢!!