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

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.

主站蜘蛛池模板: 台湾省| 利辛县| 修水县| 北碚区| 乳源| 西安市| 来凤县| 晋州市| 定结县| 阜新市| 江津市| 铁力市| 仪陇县| 怀安县| 邳州市| 靖江市| 来安县| 应城市| 平舆县| 沛县| 宜阳县| 扬中市| 呈贡县| 克什克腾旗| 徐汇区| 仁布县| 南安市| 新兴县| 三穗县| 镇沅| 瓦房店市| 商南县| 新田县| 镇巴县| 剑河县| 荆州市| 汉中市| 南澳县| 新乐市| 孟津县| 晋中市|