[英]Basic C# Unity2D movement script not functioning
Hi there fellow Overflowers! 嗨,各位同花异草! I have recently dived into C# in Unity, because I believe that it has more functionality than UnityScript, and I come from a C++ background.
我最近进入了Unity中的C#,因为我相信它比UnityScript具有更多的功能,并且我来自C ++背景。 Referencing some code from a 2D script in the Sample Assets, I have tried to create my own.
引用示例资产中2D脚本中的一些代码,我尝试创建自己的代码。 The code is below:
代码如下:
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
}
}
}
Unfortunately, the code just doesn't work. 不幸的是,该代码无法正常工作。 I get no compile errors, but any Input does not effect the current position of the character.
我没有编译错误,但是任何Input都不会影响字符的当前位置。 Should I be using transform.Translate to change the position, or stick with AddForce to a Vector2 until the character hits a maxSpeed?
我应该使用transform.Translate更改位置,还是将AddForce坚持使用Vector2,直到角色达到maxSpeed为止?
Thanks heaps :) 谢谢堆:)
I (kinda) fixed the jerky jumping issue, basically you look for the input in the Update() function which switches a Bool in FixedUpdate() 我(很高兴)解决了跳动问题,基本上,您在Update()函数中查找输入,该函数在FixedUpdate()中切换了布尔值
void Update () {
if (Input.GetKeyDown (KeyCode.Space) && playerGrounded) {
playerJumped = true;
}
}
Then in FixedUpdate() I looked for this Bool and did my AddForce 然后在FixedUpdate()中,我寻找了这个布尔并做了我的AddForce
void FixedUpdate () {
if (playerJumped) {
playerJumped = false;
rigidbody2D.AddForce (new Vector2 (0, jumpForce),ForceMode2D.Impulse);
playerGrounded = false;
}
}
Setting playerJumped to false makes sure it doesn't run several times and the player can't jump again because I also set the grounded to false. 将playerJumped设置为false可确保它不会运行几次,并且播放器不会再次跳动,因为我还将接地设置为false。 grounded gets set back to true when the player collides with things tagged "ground".
当玩家与标记为“地面”的事物发生碰撞时,grounded会恢复为true。
I'm still new to Unity and C# overall so I can't guarantee this is the best (or even a good) way. 总体而言,我还是Unity和C#的新手,所以我不能保证这是最好的方法。 Hope I could help somehow though ;)
希望我能有所帮助;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.