直接上代码:
using UnityEngine;
using System.Collections;
public class findFloor : MonoBehaviour {
private Vector3 floorPosition;
private RaycastHit hit;
private float rayLength = 300;
private float ySpeed=20;
private float gravity=2f;
private float jumpForce=1f;
public Transform character;
private uint jumpState=0; //0=grounded 1=jumping
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
DetectKeys();
FindFloorAndRotation();
MoveCharacter();
MoveCamera();
}
void DetectKeys(){
//jump:
if (Input.GetKeyDown("space") && jumpState==0) {
ySpeed-=jumpForce;
jumpState=1;
}
}
void FindFloorAndRotation(){
if (Physics.Raycast(character.position,-Vector3.up,out hit, rayLength)){
Debug.DrawRay(character.position, -Vector3.up * hit.distance);
floorPositio