在Unity中,我用這個腳本做了一個測試精靈,但是當我跳得快或者隨機時我的角色掉在了地上,但是他在移動時保持慢,但是在跳/快速着陸。我的角色一直在Unity3D中穿過障礙墜落
using UnityEngine;
using System.Collections;
public class code : MonoBehaviour {
//void FixedUpdate() {
/*if (Input.GetKey (KeyCode.LeftArrow))
{
int Left = 1;
}
if (Input.GetKey (KeyCode.RightArrow))
{
int Right = 1;
}
if (Input.GetKey(KeyCode.UpArrow))
{
}
*/
public float speed1 = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void FixedUpdate() {
CharacterController controller = GetComponent<CharacterController>();
// is the controller on the ground?
if (controller.isGrounded) {
//Feed moveDirection with input.
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
//Multiply it by speed.
moveDirection *= speed1;
//Jumping
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
if (Input.GetKey (KeyCode.UpArrow))
moveDirection.y = jumpSpeed;
}
//Applying gravity to the controller
moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
controller.Move(moveDirection * Time.deltaTime);
}
}
你是否附加了對撞機到你的地面和你的角色? –
您沒有發佈'CharacterController'的代碼。這是一個問題,因爲它看起來像'isGrounded'沒有正確標記。 – KingDan
@halfer我認爲他的意思是。 – Alox