# CSharpSdudy_2018B **Repository Path**: disaster-bitterly/CSharpSdudy_2018B ## Basic Information - **Project Name**: CSharpSdudy_2018B - **Description**: 这个是我们用来存放C#教学代码的仓库 - **Primary Language**: C# - **License**: BSL-1.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-09-20 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 叉车项目 #### 控制叉车 创建脚本绑定到叉车上 ![001](001.png) 通过根物体上的脚本控制子物体的方法 ![002](002.png) ##### 叉车运动脚本代码 ``` using System.Collections; using System.Collections.Generic; using UnityEngine; public class forklift : MonoBehaviour { public float moveSpeed = 0f; public float upDownSpeed = 0.2f;//叉子的移动速度 public Transform liftObj; public Transform Wheel_L_Back; public Transform Wheel_L_Front; public Transform Wheel_R_Back; public Transform Wheel_R_Front; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { //如果按下键盘上的上箭头,才会执行里面的代码 if (Input.GetKey(KeyCode.Q)) { if (liftObj.localPosition.y < 5.5f) { liftObj.Translate(Vector3.up * Time.deltaTime * 10f); } } if (Input.GetKey(KeyCode.E)) { if (liftObj.localPosition.y > 2.4f) { liftObj.Translate(Vector3.down * Time.deltaTime * 10f); } } float translation = Input.GetAxis("Vertical") * 3f*Time.deltaTime; print(translation); //Vertical是垂直,Horizontal是水平 //前后移动叉车 transform.Translate(transform.forward * translation); //旋转叉车 float rotation = Input.GetAxis("Horizontal") * 20f * Time.deltaTime; transform.Rotate(Vector3.up * rotation); //控制轮子移动 Wheel_L_Front.Rotate(Vector3.right * translation*50f); Wheel_L_Back.Rotate(Vector3.right * translation * 50f); Wheel_R_Front.Rotate(Vector3.right * translation * 50f); Wheel_R_Back.Rotate(Vector3.right * translation * 50f); } } ```