Unity 欢乐球球

//—1 自己控制球的弹跳力代码如下 —2) 在Project视图中给球添加Physical Materical 设置弹力以后 拉给球的Collider的Materical 属性)
SphereCollider sphereCollider;
public float bounceSpeed;
public float gravity;
public float maxSpeed = 1;
float radius = 0;
float speed;
void Start()
{
sphereCollider = GetComponent();
radius = sphereCollider.radius;
}
//球掉落移动计算
void Drop()
{
//每帧减去模拟重力带来的速度影响
speed -= gravity * Time.deltaTime;

//限制球能达到的最大速度
speed = Mathf.Clamp(speed, -maxSpeed, maxSpeed);
transform.position += new Vector3(0, speed, 0);
}
void Bounce()
{
//当球反弹回升的时候跳过检测
if (speed >= 0)
{
return;
}
//确定一个位置合适的立方体,比球略小,位置偏下
Vector3 p = transform.position + new Vector3(0, -radius, 0);
Vector3 size = new Vector3(radius * 0.5f, radius * 0.5f, radius * 0.5f);
if (Physics.OverlapBox(p, size, Quaternion.identity, LayerMask.GetMask("Ground")).Length > 0)
{
speed = bounceSpeed;
}
}
void Update()
{
Bounce();
Drop();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
}

//物体的旋转
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InputLogic : MonoBehaviour {
//记录上一帧鼠标的位置
Vector3 lastMousePos=Vector3.zero;
void TouchRotate()
{
        //鼠标左键按下时转动
        if (Input.GetMouseButton(0))
{
            //计算这一帧鼠标位置与上一帧的差值,然后以Y轴转动
            float moveX = (Input.mousePosition - lastMousePos).x;
transform.Rotate(0, -moveX, 0);
}
}
void Update()
{
TouchRotate(http://www.amjmh.com/v/BIBRGZ_558768/);
lastMousePos = Input.mousePosition;
}
}

posted @ 2019-09-04 17:09  水至清明  阅读(379)  评论(0编辑  收藏  举报