- Home /
Something Really Strange
Hey guys i am making a tps multiplayer game in unity.this is my first game in unity and overall so the problem is really simple and weird. i put this piece of code in my ammo script:-
private void FixedUpdate() { if (ammoType=="RealTime") {
transform.Translate(Vector3.forward * speed * Time.deltaTime);
if (Physics.Linecast(lastPosition, transform.position, out target,0,QueryTriggerInteraction.Ignore))
{
Debug.Log(target.transform.gameObject.name);
transform.position = target.point;
Instantiate(hitEffect, target.point, Quaternion.identity);
Debug.Log("Inst hit effect realtime");
Destroy(gameObject);
}
}
}
private void LateUpdate()
{
lastPosition = transform.position;
}
The purpose of this code is to avoid bullet through paper problem. So regardless of its speed projectile always knows when it collide with something. This code was working perfectly till yesterday, than suddenly it stopped working. im stuck with it and in all my life im unable to figure out what happened suddenly. any help regarding this would be greatly appreciated
Answer by anant6 · Aug 23, 2019 at 04:08 PM
@Danzou
1. I reset all the physics settings to default and it still doesn't work 2. Only projectiles are triggers that is the reason i chose to ignore triggers during the linecast 3. tried and it still doesnt work.this is the new function:-
private void FixedUpdate()
{
if (ammoType=="RealTime")
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
Debug.Log("realtime");
Debug.DrawLine(lastPosition, transform.position,Color.blue);
if (Physics.Linecast(lastPosition, gameObject.transform.position, out target, 0, QueryTriggerInteraction.Ignore))
{
Debug.Log(target.transform.gameObject.name);
transform.position = target.point;
Instantiate(hitEffect, target.point, Quaternion.identity);
Debug.Log("Inst hit effect realtime");
Destroy(gameObject);
}
lastPosition = transform.position;
}
}
Please any help would be great as its making me insane
I see that you have the layer$$anonymous$$ask parameter (after out target) from Linecast set to 0, try setting it to Physics.AllLayers or adding a variable Layer$$anonymous$$ask to your properties and manually set which layers you want to trigger on.
yes sir it worked just fine on all changing 0 to physics.alllayers thank you so much!! thanks thanks thanks
Answer by TurboHermit · Aug 23, 2019 at 11:47 AM
If you haven't changed anything in the code and it "suddenly" stopped working, it's very likely because something in the inspector or in settings have changed.
Since most of this is physics related, I suggest:
Checking if you changed FixedUpdate rate in physics settings.
The thing you're trying to hit is not a trigger, since you ignore querying triggers.
Try setting "lastPosition" in the end of your FixedUpdate instead of LateUpdate. FixedUpdate triggers independently (at a fixed rate) from LateUpdate (every frame), so you're currently setting your lastPosition more often than checking for it, meaning this distance is not always equal to the distance you want it to check. I'm guessing this is your current problem, but that DOES mean that you've changed something in code.
Answer by Parokonnyy · Aug 23, 2019 at 10:01 AM
Try to check what in the variable "ammoType". You can use Debug.Log(ammoType) or set the breakpoint on this line and debug to know the value. Probably, you accidentally change this variable in the code or in the inspector.
@Parokonnyy No Sir ammoType is RealTime That's the reason projectile is travelling forward if it was "HitScan" Type Then it would instantly hit the target.It just isn't detecting any collisions