
作用:wasd、上下左右
using System.Collections;using System.Collections.Generic;using UnityEngine;public class tankmovement : MonoBehaviour{public float speed = 5;private Rigidbody rigidbody;public float angularspeed = 7;public float number = 1;// Start is called before the first frame updatevoid Start(){rigidbody = this.GetComponent<Rigidbody>();}// Update is called once per framevoid Update(){float v = Input.GetAxis("VerticalPlayer"+ number);rigidbody.velocity = transform.forward * v * speed;float h = Input.GetAxis("HorizontalPlayer" + number);rigidbody.angularVelocity = transform.up * h * angularspeed;}}
using System.Collections;using System.Collections.Generic;using UnityEngine;public class tankmovement : MonoBehaviour{public float speed = 5;//前进速度private Rigidbody rigidbody;public float angularspeed = 7;//转弯速度public float number = 1;//玩家编号// Start is called before the first frame updatevoid Start(){rigidbody = this.GetComponent<Rigidbody>();}// Update is called once per framevoid Update(){float v = Input.GetAxis("VerticalPlayer"+ number);//玩家编号+wsrigidbody.velocity = transform.forward * v * speed;float h = Input.GetAxis("HorizontalPlayer" + number);//玩家编号+adrigidbody.angularVelocity = transform.up * h * angularspeed;//角度}}
1、Rigibody - freeze Position:锁y轴
2、Rigibody - freeze Rotation:锁x、z轴
3、Eidt - Project Setting - Input - Horizontal -右键复制Duplicate Array Element2次 - 命名HorizontalPlayer1、HorizontalPlayer2
4、Eidt - Project Setting - Input - Vertical -右键复制Duplicate Array Element2次 - VerticalPlayer1、VerticalPlayer2
5、Player1 - 使用wasd - 删掉上下左右键
6、Player2 - 使用上下左右键 - 删掉wasd