[英]My character won't jump. (Unity2D C#)
所以我的代码似乎工作得很好。 从动画到重力再到移动的一切,除了跳跃。 我看不出我的代码中有什么问题不允许跳转工作,这是代码:
using UnityEngine;
using System.Collections;
using System;
public class CharacterRun : MonoBehaviour
{
public float MaxSpeed = 10;
bool FacingRight = true;
Animator anim;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 700f;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool("Ground", grounded);
anim.SetFloat("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
float move = Input.GetAxis("Horizontal");
anim.SetFloat("hSpeed", Mathf.Abs(move));
GetComponent<Rigidbody2D>().velocity = new Vector2(move * MaxSpeed, GetComponent<Rigidbody2D>().velocity.y);
GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeRotation;
if (move > 0 && !FacingRight)
Flip();
else if (move < 0 && FacingRight)
Flip();
}
private void SetFloat(string v1, float v2)
{
}
void update ()
{
if (grounded && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool("Ground", false);
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
}
}
void Flip()
{
FacingRight = !FacingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
因为你需要命名函数更新而不是更新。
void Update () /// Not update
{
if (grounded && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool("Ground", false);
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.