unity案例RollABall(2)之创建游戏玩家

 创建一个球体,作为游戏主角,命名为player,同样创建材质球:

给物体添加一个刚体组件:

接下来,编写一个脚本代码,控制游戏物体的行走:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class move : MonoBehaviour
{
    // Start is called before the first frame update
    public Rigidbody rd;  //定义一个属性 在面板上面就有了  就可以赋值刚体  也可以通过代码赋值

    void Start()
    {
        rd = GetComponent<Rigidbody>();


    }

    // Update is called once per frame
    void Update()
    {
        float h=Input.GetAxis("Horizontal");
        float v=Input.GetAxis("Vertical");
        rd.AddForce(new Vector3(h,0,v)*10);

    }
}

猜你喜欢

转载自blog.csdn.net/qq_51196701/article/details/123017629
今日推荐