官术网_书友最值得收藏!

Initialization

In the following code, we can see our TankController class. First, we set up our Start function and the Update functions.

The code in the PlayerTankController.cs file is as follows:

using UnityEngine; 
using System.Collections; 
 
public class PlayerTankController : MonoBehaviour 
{ 
    public GameObject Bullet; 
 
    private Transform Turret; 
    private Transform bulletSpawnPoint; 
    private float curSpeed, targetSpeed, rotSpeed; 
    private float turretRotSpeed = 10.0f; 
    private float maxForwardSpeed = 300.0f; 
    private float maxBackwardSpeed = -300.0f; 
 
    //Bullet shooting rate 
    protected float shootRate = 0.5f; 
    protected float elapsedTime; 
 
    void Start() 
    { 
 
      //Tank Settings 
      rotSpeed = 150.0f; 
 
      //Get the turret of the tank 
      Turret = gameObject.transform.GetChild(0).transform; 
      bulletSpawnPoint = Turret.GetChild(0).transform; 
    } 
 
    void Update() 
    { 
      UpdateWeapon(); 
      UpdateControl(); 
    } 

The first—and only—child object of our tank entity is the Turret object, and the first—and only—child of the Turret object is the bulletSpawnPoint. The Start function finds these objects and then assigns them to their respective variables. Later, after we create the Bullet object, we assign it from the inspector. Also, we included the Update function that calls our UpdateControl and UpdateWeapon functions, which we will create soon.

主站蜘蛛池模板: 阜阳市| 鄯善县| 汝州市| 鹤壁市| 柳江县| 临漳县| 虞城县| 沐川县| 武鸣县| 乐昌市| 麦盖提县| 白银市| 宝坻区| 横山县| 大丰市| 镇赉县| 西乡县| 南乐县| 敖汉旗| 南丹县| 松阳县| 南乐县| 健康| 纳雍县| 阜城县| 广南县| 栾城县| 项城市| 顺平县| 芦山县| 尚义县| 当阳市| 台北县| 定远县| 杭州市| 淮安市| 延长县| 资兴市| 宜昌市| 衡阳县| 小金县|