小球移动与跳跃

1.创建一个宽敞的平面。

2.添加小球模型,设置Rigidbody

3.给小球添加移动代码

public class move: MonoBehaviour
{
    public Rigidbody rg;
    // Start is called before the first frame update
    void Start()
    {
        rg = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
       Vector3 m  = new Vector3(h, 0, v);
        transform.rotation = Quaternion.LookRotation(m);
    }
}
 

4.将地面tag更改为Ground。

5.创建跳跃代码,在代码中关联地面tag,设置各种参数。

public class jump : MonoBehaviour
{
    public float moveSpeed = 5f;
    // 旋转速度
    public float rotateSpeed = 5f;
    // 跳跃速度
    public float jumpSpeed = 8f;
    // 是否在地上
    private bool isGround = true;
    Vector3 moveAmount;
    Rigidbody rb;
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        
    }
    private void FixedUpdate()
    {
        rb.MovePosition(rb.position + moveAmount);
    }
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Ground")
        {
            isGround = true;
        }
    }


}

  • 14
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值