Java教程

Unity2020 控制刚体运动

本文主要是介绍Unity2020 控制刚体运动,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.代码

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class Player : MonoBehaviour
 6 {
 7     public float speed = 1;
 8     private Rigidbody rd;
 9 
10     // Start is called before the first frame update
11     void Start()
12     {
13         rd = GetComponent<Rigidbody>();
14     }
15 
16     // Update is called once per frame
17     void Update()
18     {
19         float h = Input.GetAxis("Horizontal");
20         rd.AddForce(new Vector3(h, 0, 0) * speed);
21         float v = Input.GetAxis("Vertical");
22         rd.AddForce(new Vector3(0, 0, v) * speed);
23     }
24 }

2.设置

 

 

 

这篇关于Unity2020 控制刚体运动的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!