基本C#Unity2D移动脚本无法运行
嗨,各位飞越者! 我最近在统一中跳入C++中,因为我相信它比UNITScript更具功能性,而我来自C++背景。引用示例资源中2D脚本中的一些代码,我尝试创建自己的代码。代码如下:基本C#Unity2D移动脚本无法运行,c#,unity3d,2d,game-physics,C#,Unity3d,2d,Game Physics,嗨,各位飞越者! 我最近在统一中跳入C++中,因为我相信它比UNITScript更具功能性,而我来自C++背景。引用示例资源中2D脚本中的一些代码,我尝试创建自己的代码。代码如下: using UnityEngine; public class PlayerControl : MonoBehaviour { //Variables [HideInInspector] public bool facingForward = true; //for Flip() Funct
using UnityEngine;
public class PlayerControl : MonoBehaviour {
//Variables
[HideInInspector]
public bool facingForward = true; //for Flip() Function
bool isGround = true; //for Grounded() Function
public float maxSpeed = 5.0f; //Terminal sideways velocity
public float HorizonAxis; //Checks for a/d movement
public float jumpFloat = 1000.0f; //Modular, use Unity Editor
public float moveFloat = 400.0f; // " "
void Start() {
//transform.position(0,0,0);
}
void Flip() {
facingForward = !facingForward; //switches boolean
Vector3 theScale = transform.localScale; //assigns vector to localscale of Player
theScale.x *= -1; //if x = 1, position becomes -1 and thus flips
transform.localScale = theScale; //reassigns the localscale to update theScale
}
bool Grounded() {
if (transform.position.y > 1) { //if position of gameObject is greater that 1, not grounded
isGround = false;
} else {
isGround = true;
}
return isGround; //function returns true or false for isGround
}
void Update() {
HorizonAxis = /*UnityEngine.*/Input.GetAxis ("Horizontal"); //assigns HorizonAxis to a/d movement from UnityEngine.Input
if (HorizonAxis * rigidbody2D.velocity.x > maxSpeed) { // if Input a/d by current x velocity of gameObject is greater than maxSpeed
rigidbody2D.velocity = new Vector2 (Mathf.Sign (rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y); //1 or -1 times the max speed, depending on direction
}
else if (HorizonAxis * rigidbody2D.velocity.x < maxSpeed) { //if Input a/d is less than terminal velocity
rigidbody2D.AddForce(Vector2.right * HorizonAxis * moveFloat); //add force to the right equivilant to Input by scalar moveFloat
}
if (Input.GetButtonDown ("Jump")) { //If Space
if(isGround) { //and isGround returns true
rigidbody2D.AddForce(new Vector2(0.0f, jumpFloat)); //add upwards force to bottom of rigidbody2D
isGround = false; //Resets isGround value
}
}
if (HorizonAxis > 0 && !facingForward) {//if UnityEngine.Input is to the right and facing left
Flip (); //execute
}
else if (HorizonAxis < 0 && facingForward) { //else
Flip (); //execute
}
}
}
使用UnityEngine;
公共类玩家控制:单一行为{
//变数
[hideininstecpt]
public bool facingfroward=true;//对于Flip()函数
bool isGround=true;//对于接地()函数
公共浮点maxSpeed=5.0f;//终端侧向速度
公共浮动轴;//检查a/d移动
公共浮点jumpFloat=1000.0f;//模块化,使用Unity编辑器
公共浮点数moveFloat=400.0f;/“”
void Start(){
//变换位置(0,0,0);
}
void Flip(){
facingForward=!facingForward;//开关布尔值
Vector3 theScale=transform.localScale;//将向量分配给播放器的localScale
scale.x*=-1;//如果x=1,位置变为-1,因此翻转
transform.localScale=theScale;//重新分配localScale以更新该比例
}
布尔{
若(transform.position.y>1){//若游戏对象的位置大于1,则不固定
isGround=false;
}否则{
isGround=真;
}
return isGround;//函数为isGround返回true或false
}
无效更新(){
HorizonAxis=/*UnityEngine.*/Input.GetAxis(“水平”);//将HorizonAxis分配给UnityEngine.Input中的a/d移动
如果(HorizonAxis*rigidbody2D.velocity.x>maxSpeed){//如果游戏对象当前x速度的输入a/d大于maxSpeed
rigidbody2D.velocity=新矢量2(数学符号(rigidbody2D.velocity.x)*最大速度,rigidbody2D.velocity.y);//最大速度的1倍或-1倍,具体取决于方向
}
否则,如果(水平轴*rigidbody2D.velocity.x0&&!朝向前方){//if UnityEngine。输入位于右侧,面向左侧
Flip();//执行
}
else如果(水平轴<0且朝向前方){//else
Flip();//执行
}
}
}
不幸的是,代码根本不起作用。我没有得到编译错误,但是任何输入都不会影响字符的当前位置。我应该使用transform.Translate来更改位置,还是使用AddForce将其粘贴到矢量2,直到角色达到最大速度
感谢堆:)I(有点)修复了跳跃问题,基本上您可以在Update()函数中查找输入,该函数在FixedUpdate()中切换Bool
然后在FixedUpdate()中,我查找这个Bool并执行AddForce
void FixedUpdate () {
if (playerJumped) {
playerJumped = false;
rigidbody2D.AddForce (new Vector2 (0, jumpForce),ForceMode2D.Impulse);
playerGrounded = false;
}
}
将playerJumped设置为false可以确保它不会运行多次,并且玩家不能再次跳跃,因为我还将grounded设置为false。当玩家与标记为“地面”的物体发生碰撞时,“地面”将恢复为真
总的来说,我对Unity和C#还不熟悉,所以我不能保证这是最好的(甚至是好的)方式。希望我能帮上忙;) 你有没有试过使用MonoDevelop逐步完成代码?你的脚本适合我。还有其他原因导致它不能移动吗?我注意到播放器和脚本之间有一个链接错误,我解决了这个问题。我现在遇到的问题是,这些控件非常不稳定,不能正常工作,有什么建议吗?在处理刚体和力时,应该使用FixeUpdate()而不是Update()。请看这里的统一描述:这对左右移动有很大帮助。当角色处于半空中时,我无法对其施加和强制(x轴运动不符合y轴),并且我无法使其保持直立,这一事实使我产生了问题:(
void FixedUpdate () {
if (playerJumped) {
playerJumped = false;
rigidbody2D.AddForce (new Vector2 (0, jumpForce),ForceMode2D.Impulse);
playerGrounded = false;
}
}