Directly run the Code:
[Csharp]
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 );
FloorPosition = hit. point;
}
}
Void MoveCharacter (){
// Add gravity:
YSpeed + = gravity * Time. deltaTime;
// Apply gravity:
Character. position = new Vector3 (floorPosition. x, character. position. y-ySpeed, floorPosition. z );
// Floor checking:
If (character. position. y <floorPosition. y ){
YSpeed = 0;
JumpState = 0;
Character. position = new Vector3 (floorPosition. x, floorPosition. y, floorPosition. z );
}
}
Void MoveCamera (){
// Method 1
Camera. main. gameObject. transform. LookAt (character. position );
ITween. MoveUpdate (Camera. main. gameObject, new Vector3 (character. position. x, character. position. y, character. position. z-100f),. 9f );
// Method 2
// ITween. LookUpdate (gameObject, target. position, 2 );
}
}
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 );
FloorPosition = hit. point;
}
}
Void MoveCharacter (){
// Add gravity:
YSpeed + = gravity * Time. deltaTime;
// Apply gravity:
Character. position = new Vector3 (floorPosition. x, character. position. y-ySpeed, floorPosition. z );
// Floor checking:
If (character. position. y <floorPosition. y ){
YSpeed = 0;
JumpState = 0;
Character. position = new Vector3 (floorPosition. x, floorPosition. y, floorPosition. z );
}
}
Void MoveCamera (){
// Method 1
Camera. main. gameObject. transform. LookAt (character. position );
ITween. MoveUpdate (Camera. main. gameObject, new Vector3 (character. position. x, character. position. y, character. position. z-100f),. 9f );
// Method 2
// ITween. LookUpdate (gameObject, target. position, 2 );
}
}