【发布时间】:2015-02-20 14:15:15
【问题描述】:
我目前正在使用 Unity 开发一个项目,由于我对 C# 和编程还很陌生,因此我遇到了一些问题。第一个问题是我的玩家的碰撞检测 - 我有一个带有重力的建模刚体和一个附加的(非凸面)网格碰撞器。对于我的墙,它们是带有盒子碰撞器的导入模型。
如果我让我的盒子碰撞器保持模型的默认大小(大约是玩家的两倍厚),那么玩家有时会通过它并飞出另一边。为了解决这个问题,我只是让对撞机更大,以适应墙壁/环境,但如果我希望玩家能够四处走动或在墙壁的两侧,这并不理想。目前玩家在碰撞时会“爬上”墙的一侧,这不应该发生吗?我想知道我是否配置错误,或者完全通过脚本管理玩家的碰撞是否会更好?我在下面有我的角色和玩家组件的屏幕截图: 墙 - http://i.imgur.com/YRdTgSh.png? 播放器 - http://i.imgur.com/DVKOdG1.png?
我的第二个问题是当我尝试检查我的播放器是否接地以让他们跳跃时。下面的代码是我当前用于管理玩家移动和跳跃的整个移动脚本。目前,玩家可以无限跳高,但是如果我从高处生成玩家,他们只会跳一次,直到达到一定高度,然后他们会再次重复跳跃,直到空间被释放。
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
// Update is called once per frame
void FixedUpdate() {
// Creating floats to hold the speed of the plater
float playerSpeedHorizontal = 4f * Input.GetAxis ("Horizontal");
float playerSpeedVertical = 4f * Input.GetAxis ("Vertical");
// Transform statements to move the player by the playerSpeed amount.
transform.Translate (Vector3.forward * playerSpeedVertical * Time.deltaTime);
transform.Translate (Vector3.right * playerSpeedHorizontal * Time.deltaTime);
// Calling the playerJump function when the jump key is pressed
if (Input.GetButton("Jump"))
{
playerJump();
Debug.Log ("Can jump");
}
}
/// Here we handle anything to do with the jump, including the raycast, any animations, and the force setting it's self.
void playerJump() {
const float JumpForce = 1.0f;
Debug.Log ("Should Jump");
if(Physics.Raycast(rigidbody.position, Vector3.up, collider.bounds.extents.y + 0.1f)) {
// Debug.Log ("Can jump");
rigidbody.AddForce (Vector3.up * JumpForce, ForceMode.VelocityChange);
}
}
}
如果有人可以提供帮助,我将不胜感激,因为我对此感到非常困惑
【问题讨论】: