Hey guys, first time poster so I’m not sure if this is the right place to post this. Anyway, I’m working on a singleplayer RTS/FPS conquest type game (mostly for learning purposes) in which two factions are battling for control of a certain location.
I’ve been working on the actual zone capture mechanics and I’ve hit somewhat of a logic roadblock (not sure if it’s because it’s too early in the morning and I’ve only just started drinking a cup of coffee lol). What I’m trying to achieve at the moment is the having the flag change position as the control of the location begins to shift from one team to the other.
So let’s say that Team A is the current controller of the location and Team B is capturing it, and the flag’s max height is 10 on y axis and it’s lowest is 1; I want the flag to begin moving downwards from max height to min height as Team B’s capture % goes up from 0% to 50% at which point the flag will switch textures and then begin to ascend again as Team B’s capture % goes up from 50% to 100%. Similar to how Bf 4 works. So if you guys can walk me through the logic of the best way to achieve this that would be awesome!
I’ve posted my current script below. Not sure if this is the most optimal way of going about it so any suggestions and advice would be much appreciated.
using UnityEngine;
using System.Collections;
public class ConquestZoneTest : MonoBehaviour {
public GameObject flag;
public Texture2D flagTeamA;
public Texture2D flagTeamB;
public Light flagLight;
public float lightMaxIntensity = 5.0f;
public float lightMinIntensity = 0.0f;
public float lightChangeMargin = 0.2f;
public float lightSwitchRate = 1.0f;
public float captureRate = 2.0f;
public bool teamACapturing = false;
public bool teamBCapturing = false;
public bool strategicPointContested = false;
public bool underAControl = false;
public bool underBControl = false;
public float teamACapPercent = 0.0f;
public float teamBCapPercent = 0.0f;
private MeshRenderer flagRenderer;
private float targetLightIntensity;
void OnEnable() {
flagRenderer = flag.GetComponent<MeshRenderer> ();
flagLight.intensity = 0.0f;
}
void Update () {
if (teamACapturing == true && strategicPointContested == false) {
teamACapPercent += Time.deltaTime * captureRate;
teamBCapPercent = 100 - teamACapPercent;
}
if (teamBCapturing == true && strategicPointContested == false) {
teamBCapPercent += Time.deltaTime * captureRate;
teamACapPercent = 100 - teamBCapPercent;
}
if (teamACapturing == true && teamBCapturing == true) {
teamACapPercent = teamACapPercent;
teamBCapPercent = teamBCapPercent;
strategicPointContested = true;
} else {
strategicPointContested = false;
}
if (teamACapPercent >= 100) {
teamACapPercent = 100;
if (underAControl == false) {
flagRenderer.material.mainTexture = flagTeamA;
underBControl = false;
underAControl = true;
}
}
if (teamACapPercent <= 0) {
teamACapPercent = 0;
}
if (teamBCapPercent >= 100) {
teamBCapPercent = 100;
if (underBControl == false) {
flagRenderer.material.mainTexture = flagTeamB;
underAControl = false;
underBControl = true;
}
}
if (teamBCapPercent <= 0) {
teamBCapPercent = 0;
}
if ((teamACapPercent > 0 && teamACapPercent < 100) || (teamBCapPercent > 0 && teamBCapPercent < 100) || strategicPointContested) {
flagLight.intensity = Mathf.Lerp (flagLight.intensity, targetLightIntensity, lightSwitchRate * Time.deltaTime);
CheckLightTargetIntensity ();
} else {
flagLight.intensity = Mathf.Lerp (flagLight.intensity, 0.0f, lightSwitchRate * Time.deltaTime);
}
}
void CheckLightTargetIntensity(){
if (Mathf.Abs (targetLightIntensity - flagLight.intensity) < lightChangeMargin) {
if (targetLightIntensity == lightMaxIntensity)
targetLightIntensity = lightMinIntensity;
else
targetLightIntensity = lightMaxIntensity;
}
}
}