The turret rotation bug record of the tank project——Unity's notes (2020.11.19)

I solved a bug today, write a blog to record it.
Pre-explanation, in fact, this bug cannot be solved by myself, because to be honest, I didn't understand why this bug was triggered until it was solved. Here, please allow me to post the work of the big guy who helped me solve the problem:
Unity3D: Gun Turret Rotation (Download in Description) is
really a treasure project, I can learn a lot.
The project downloaded from the GitHub link at the bottom of his video helped me find the problem, that is, his
The rotation of this world coordinate caused all this
BUG grew into this: The real combat effectiveness of the US Army Sherman in World War II (bushi)
Yes, because I was so fascinated, I posted a video to commemorate it. To describe it in words, this rotating turret is written in accordance with the code on the picture. As long as the tank body rotates, the turret will continue to rotate like a helicopter. As for why this bug occurs?
It didn’t exist before. Refer to the notes on No. 16, but it turned out to be problematic. The problem is that the turret rotates around the world coordinates, and my code prevents the turret from rotating along the x and z axis, which leads to the tank turret. Always maintain the level of the world axis, even if the tank is climbing or overturning. Forget the screenshot, it's probably such a situation:
This is probably the case
In fact, I can easily think of the way to solve the problem, which is to change the world coordinates to local coordinates. As a result, the BUG mentioned at the beginning of the article appeared.
At that time, it really made me feel uncomfortable, because I really couldn't find out where the problem was, and I spent a whole night of useless work. Facts have proved that when you encounter a problem that can't be found, you still have to look for a well-made project to take a look.
Of course, I thought the turret rotation had already been done. I started to fire. The effect is barely enough. With rigidbody shells, the tank has made a few more collision boxes, and tried to mark the trajectory with DrawLine in the Scene panel. There were also some small bugs. For example, I set the speed of the cannonball in FixedUpdate, which caused the speed of the cannonball to remain unchanged. Then I wondered why the trajectory of the cannonball did not fall, and why it circled after the collision. . .
Of course, the shells do not use the object pool. I have forgotten how to do the object pool. It may be too late to learn now, so I will add it later. At present, it is estimated that there is little impact on the performance of the project without using the object pool, and there are not many shells after all. GC should not appear frequently;
besides, I made an effect that the tank receives a reaction force every time the fire is fired. It may not be realistic, but it is better than nothing.
Now that the problem is solved, I will optimize the rotation of the barrel for the rest of the evening.

Then the next task list is:

  • Aiming system. I really can’t make a UI
  • The collision of shells. If it’s simpler, it will count as a random number. If it’s more complicated, try to see if it can be counted as an angle.
  • Tank explosion effect
  • The enemy's AI must at least attack the player. It would be great to be able to move.
  • Ballistic visualization (this is the last one, the big deal is made into a self-luminous material for cannonballs)
  • High-speed object collision detection (use rays to detect once in each frame)
  • Injury algorithm, with random numbers (you may need to refer to FOW)
  • Collision angle (judged by the vector from the position of the shell to the position of the enemy? Seems not to work)
  • How to aim the enemy (don’t know yet, haven’t done it)
  • How do we aim? (The big deal is to add a UI and it’s over, you can’t blame me)
  • Switching between the first person and the third person (how many cameras do you switch back and forth?)
  • How do I know whether the hit is the front armor, the side armor or the rear armor (judged by the direction of the shell and the direction of the opponent? But it doesn't seem to be too good, just don’t distinguish between front and rear armor)

Guess you like

Origin blog.csdn.net/qq_37856544/article/details/109826411