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

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.

主站蜘蛛池模板: 张家港市| 修水县| 依安县| 抚顺县| 江永县| 大悟县| 江永县| 江永县| 余江县| 邵武市| 宿松县| 宣城市| 鸡西市| 紫云| 紫云| 舞阳县| 福清市| 屯留县| 民和| 晴隆县| 溆浦县| 运城市| 栾川县| 长子县| 施秉县| 双城市| 赫章县| 青阳县| 香格里拉县| 龙山县| 武宁县| 德钦县| 平陆县| 阳谷县| 舟曲县| 福建省| 道真| 集安市| 莱州市| 临高县| 剑川县|