Please help me figure this out
Hi hope someone can help, new to coding and can't seem to find an answer online, probably because am not really sure how to explain it so here goes...
am making a 3d space game and need my AI ships to make low speed manuovers to dock etc..
the movement works fine but can't figure out how to determine which reaction thruster effect shoud fire
have some very rudimentary particle effects as Reaction control thrusters which are controlled by an effects script. In my Stabiliziation Script wich works perfectly i use Rb.local velocity but the problem is the direction is in vector3 not transform
... hope that kind of makes sense
Captain captain;
public float enginePower, rcsPower, speedCurrent, speedSelected, rotationSpeed, engineThrust, rcsThrust, avoidanceWidth;
public bool isBurning, isStrafeUp, isStrafeDown, isStrafeLeft, isStrafeRight, isStrafeFwd, isRetroBurning, headingFound;
public Vector3 locVel, manuoverDirection;
public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
captain = GetComponent<Captain>();
}
// Update is called once per frame
void Update()
{
if (captain.stop)
{
Stop();
speedSelected = 0;
engineThrust = 2;
}
else if (captain.crawl)
{
speedSelected = enginePower;
engineThrust = .5f;
}
else if (captain.cruise)
{
speedSelected = enginePower * 2;
engineThrust = 1;
}
else if (captain.fullAhead)
{
speedSelected = enginePower * 4;
engineThrust = 2;
}
}
private void FixedUpdate()
{
locVel = transform.InverseTransformDirection(rb.velocity);
speedCurrent = locVel.magnitude;
}
public void Stabilize() { rcsThrust = 1;
if (speedCurrent > 0)
{
float x = locVel.x;
float y = locVel.y;
float z = locVel.z;
Vector3 stabilizeDirection = Vector3.zero;
if (x < 0)
{
isStrafeRight = ShouldFireThrusters(x);
if (isStrafeRight) stabilizeDirection += transform.right;
}
else if (x > 0)
{
isStrafeLeft = ShouldFireThrusters(x);
if (isStrafeLeft) stabilizeDirection -= transform.right;
}
if (y < 0)
{
isStrafeUp = ShouldFireThrusters(y);
if (isStrafeUp) stabilizeDirection += transform.up;
}
else if (y > 0)
{
isStrafeDown = ShouldFireThrusters(y);
if (isStrafeDown) stabilizeDirection -= transform.up;
}
if (speedSelected == 0) // if stopping
{
if (z < 0)
{
isStrafeFwd = ShouldFireThrusters(z);
if (isStrafeFwd) stabilizeDirection += transform.forward;
}
else if (z > 0)
{
isRetroBurning = ShouldFireThrusters(z);
if (isRetroBurning) stabilizeDirection -= transform.forward;
}
}
rb.AddForce(stabilizeDirection * rcsPower * rcsThrust * Time.fixedDeltaTime);
public void SlowManuover() {
Vector3 direction = transform.position - captain.targetCurrent.transform.position + captain.destinationOffset;
float x = direction.x;
float y = direction.y;
float z = direction.z;
manuoverDirection = Vector3.zero;
if (x < 0)
{
manuoverDirection += Vector3.right;
}
else if (x > 0)
{
manuoverDirection -= Vector3.right;
}
if (y < 0)
{
manuoverDirection += Vector3.up;
}
else if (y > 0)
{
manuoverDirection -= Vector3.up;
}
if (z < 0)
{
manuoverDirection += Vector3.forward;
}
else if (z > 0)
{
manuoverDirection -= Vector3.forward;
}
rb.AddForce(manuoverDirection * rcsPower * rcsThrust * Time.fixedDeltaTime);
}
Helm helm;
public List<ParticleSystem> thrustersMain, rcsDwFwd, rcsDwBk, rcsUpFwd, rcsUpBk, rcsLxFwd, rcsLxBk, rcsRxFwd, rcsRxBk; // visual effects
public ParticleSystem[] fxList;
// Start is called before the first frame update
void Start()
{
helm = GetComponent<Helm>();
thrustersMain = new List<ParticleSystem>();
rcsDwFwd = new List<ParticleSystem>();
rcsDwBk = new List<ParticleSystem>();
rcsLxBk = new List<ParticleSystem>();
rcsLxFwd = new List<ParticleSystem>();
rcsRxBk = new List<ParticleSystem>();
rcsRxFwd = new List<ParticleSystem>();
rcsUpBk = new List<ParticleSystem>();
rcsUpFwd = new List<ParticleSystem>();
fxList = GetComponentsInChildren<ParticleSystem>();
foreach (ParticleSystem fx in fxList)
{
if (fx.tag == "ThrusterMain")
{
thrustersMain.Add(fx);
}
else if (fx.tag == "RcsUpFwd")
{
rcsUpFwd.Add(fx);
}
else if (fx.tag == "RcsDwFwd")
{
rcsDwFwd.Add(fx);
}
else if (fx.tag == "RcsUpBk")
{
rcsUpBk.Add(fx);
}
else if (fx.tag == "RcsDwBk")
{
rcsDwBk.Add(fx);
}
else if (fx.tag == "RcsLxFwd")
{
rcsLxFwd.Add(fx);
}
else if (fx.tag == "RcsLxBk")
{
rcsLxBk.Add(fx);
}
else if (fx.tag == "RcsRxFwd")
{
rcsRxFwd.Add(fx);
}
else if (fx.tag == "RcsRxBk")
{
rcsRxBk.Add(fx);
}
// add more Fx here !!!!!!!!
}
}
private void Update()
{
if (helm.isBurning) // main thrusters
{
foreach(ParticleSystem t in thrustersMain)
{
t.Play();
}
}
else
{
foreach (ParticleSystem t in thrustersMain)
{
}
}
if (helm.isStrafeUp) // strafe up
{
foreach (ParticleSystem b in rcsDwBk)
{
b.Play();
}
foreach (ParticleSystem f in rcsDwFwd)
{
f.Play();
}
}
else
{
foreach (ParticleSystem b in rcsDwBk)
{
}
foreach (ParticleSystem f in rcsDwFwd)
{
}
}
if (helm.isStrafeDown) // strafe down
{
foreach (ParticleSystem b in rcsUpBk)
{
b.Play();
}
foreach (ParticleSystem f in rcsUpFwd)
{
f.Play();
}
}
else
{
foreach (ParticleSystem b in rcsUpBk)
{
}
foreach (ParticleSystem f in rcsUpFwd)
{
}
}
if (helm.isStrafeLeft) // strafe left
{
foreach (ParticleSystem b in rcsRxBk)
{
b.Play();
}
foreach (ParticleSystem f in rcsRxFwd)
{
f.Play();
}
}
else
{
foreach (ParticleSystem b in rcsRxBk)
{
}
foreach (ParticleSystem f in rcsRxFwd)
{
}
}
if (helm.isStrafeRight) // strafe right
{
foreach (ParticleSystem b in rcsLxBk)
{
b.Play();
}
foreach (ParticleSystem f in rcsLxFwd)
{
f.Play();
}
}
else
{
foreach (ParticleSystem b in rcsLxBk)
{
}
foreach (ParticleSystem f in rcsLxFwd)
{
}
}
}
}
Your answer
Follow this Question
Related Questions
Slow down a character while they are midair while keeping their original velocity 0 Answers
Cant Get a Bullet to Shoot (C#) 2 Answers
How do I ignore gravity when using Rigidbody and always have the same jump force? 1 Answer
How can I move an object in the direction another object is facing. 1 Answer
Bullet shooting not working 0 Answers