Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] Raycast melee framerate issue

Discussion in 'Physics' started by MoxenGames, Oct 15, 2017.

  1. MoxenGames

    MoxenGames

    Joined:
    Oct 20, 2013
    Posts:
    8
    [SOLVED]: Switching the character Animator to Animate Physics fixed the issue. Now the weapons make consistent, beautiful arcs no matter how much lag there is.


    In my game, I immediately ran into the issue of the melee animations and bullets going way faster than what colliders could detect, so I implemented a raycast system that tracks the previous point and then casts a ray to that point in the next frame. It's the same basic setup as what others have discussed in the forums, using the popular "don't go through me" script's code.

    Now, this detects hits much better than the colliders, but I'm still having issues once the framerate drops.

    The following ugly graphic shows the issue:




    Once the framerate starts dipping, the melee attacks start missing because there are so few frames.

    Lowering the maximum time step in the Time settings fixed the hit detection, but at least in my initial tests the game ran way too slow with that to seriously consider.

    Any ideas what I can do to fix this?
     
    Last edited: Oct 17, 2017
  2. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    823
    Are you running your checks in FixedUpdate()? Instead of four rays you could also use a CapsuleCast (which ends up as a moving capsule because it is a sweep).
    I would also consider to not just check the last position to this position (which has quite some flaws as seen in your graphic), but maybe finding a way to check the arc the sword traveled from the last to this frame.
     
  3. MoxenGames

    MoxenGames

    Joined:
    Oct 20, 2013
    Posts:
    8
    Yes, I switched to FixedUpdate recently, which helped with a few things but doesn't seem to help with this issue (In fact it seems to be worse when framerate dips).

    I thought about using some sphere casting potentially, but looking at the traced lines in bad framerates, I don't think that would be enough (assuming I'm casting out from the weapon's blade). When the first frame is at relax position, and next frame is at literally the end of the swing, the two positions are incredibly far away from the enemy, with the frames between that were closer to the enemy having been skipped.

    Maybe I could estimate the arc using the previous and next rotations of the hand... I'd have to do multiple raycasts though.
     
  4. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    "Switching" to fixed update sounds like a definite culprit, if not of this problem, definitely of others. You should only use fixed update if your very aware of when and how often your code should execute.

    Just "putting" or "switching" to it will not "fix" problems, physics doesnt work that way. Your fixed update, is happening as it says, a fixed number of times a second.

    I recommend before you try and resolve this, you take a look at these topics or you will struggle to understand why this is happening in the long run:

    http://answers.unity3d.com/questions/245028/question-about-update-vs-fixedupdate.html

    https://unity3d.com/learn/tutorials/topics/scripting/update-and-fixedupdate

    http://answers.unity3d.com/questions/949222/is-raycast-efficient-in-update.html

    http://answers.unity3d.com/questions/1024589/multiple-raycasts-in-same-fixedupdate.html

    http://answers.unity3d.com/questions/266944/raycast-problem-on-high-speed.html

    http://answers.unity3d.com/questions/1141633/why-does-fixedupdate-work-when-update-doesnt.html

    P.S it took me about 10 seconds to find those, and at least 2 of those links directly answer why this is happening. One of the best optimizations you can make is to the way you search for related info for problems. Good searching = less time spent finding solutions = profit.
     
  5. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    P.S 2 : Using raycasts like this for a melee system seems like a bad way of doing this by the way. I would use some of the cast methods from the Physics class in the Unity API