Tanks Tutorial - dust trails not emitted on MovePosition
I'm a newbie - working on the Tanks tutorial. Have been following along fine, but my tank dust trails are not working when the tank is moving forward and backward. The trails emit fine during the turning operation. I suspect this has to do with the difference between MovePosition() and MoveRotation() and their affect on the particle system. I have tried virtually every setting on the particle system - disabling all modules except the renderer and the emission over distance. When dragging the particle system in the scene view, the emissions appear correctly. But, when I click play and use the keyboard to control the tank, the particles only emit on turning - not on movement.
Using 5.5.0p3
After a lot of research I figured it out, but first I want to say a couple of useful thingst to learn:
See the modern Unity on the $$anonymous$$ovePosition function: https://docs.unity3d.com/ScriptReference/Rigidbody.$$anonymous$$ovePosition.html
Notice in particular "If the rigidbody has is$$anonymous$$inematic set false then it works differently. It works like transform.position=newPosition and teleports the object (rather than a smooth transition)."
In our game, this is the case. As such, we are not "moving" our tank forwards and back, rather we teleport it s tiny bit forwards and back. Therefore, the "velocity" of the tank (and therefore teh dust) at any infintesimal moment is 0. Because the tank is either just teleported and now taking a quick break, or is having a break while it waits to be teleported again.
This explains the problem.
After a lot of work on trying to manuelly "set" the velocity of the dust, I realised it was impossible (or at least, WAY too hard) because the computer always thinks the tank is at velocity (0,0,0).
The solution is either (hard) to encode a function into the dust that records the positions of the tank iver the last 30 frames or so, and works out the velocity that way....
OR (easy -- and better) look at the Particle System component of your dust objects (both left and right) and scroll to "Emiter Velocity". Change it from Rigidbody to Transform.
That solves it.
I'm sorry for not giving the answer so quickly, but I hope you learned a bit from this (and thank for as I spent over an hour on it this afternoon trying to work it out!).
Answer by GregoryFenn · Feb 20, 2018 at 08:49 AM
Look at the Particle System component of your dust objects (both left and right) and scroll to "Emiter Velocity". Change it from Rigidbody to Transform.
Great! Thanks! Changing the Emitter Velocity to Transform solves the problem! No need to modify the Tank $$anonymous$$ovement script...
Answer by DanielePattuzzi · Dec 31, 2016 at 03:13 PM
I am facing the same issue right now. I'm a newbie too, but it seems that MovePosition() doesn't affect the particles anymore (from version 5.5). I'm not sure but the Rigidbody velocity's properties doesn't update if the tank is moved by MovePosition(). Instead, using the AddForce() method the particles effect works (affecting the velocity property).
I've found this solution that seems to work. I've modified the tank $$anonymous$$ove() method in this way:
private void $$anonymous$$ove()
{
// Adjust the position of the tank based on the player's input.
m_Rigidbody.velocity = transform.forward * m_$$anonymous$$ovementInputValue * m_Speed;
}
Interesting. Thanks for the reply. I am curious as to whether or not this is a feature or a bug. At least I know I'm not crazy!
I tried your script and it worked in resolving the issue with the particle effect not working when not turning, but I found later on that it interfered with the tank's movement after shells hit close to it. Tank seemed to stop too soon. I tried all sorts of tweaks on the rigidbody, but eventually just went back to using the original Tank$$anonymous$$ovement.cs.
Answer by SolsticeTeamGames · Sep 05, 2017 at 02:13 AM
Looks like you want to change the emitter velocity to measure Transform instead of Rigidbody, likely because MovePosition doesn't alter the Rigidbody velocity.
Answer by nbLurkerAbove · Dec 12, 2017 at 07:50 AM
Disclaimer: At the time of writing, I have only completed up to the 5th section of the tutorial.
Using @DanielePattuzzi 's answer, I've fixed the problem of the explosion not pushing the tanks backwards enough, though I modify the game's behavior in the process. I believe the problem was that the modified move function (shown below) was overwriting the tanks' velocity from the explosion, resulting in the tank stopping after 1 physics tick.
private void Move()
{
// Adjust the position of the tank based on the player's input.
m_Rigidbody.velocity = transform.forward * m_MovementInputValue * m_Speed;
}
My solution was to have the move function modify the velocity instead of overwrite it.
public float m_maxSpeed = 10f;
public float m_Acceleration = 0.5f;
private void Move()
{
// Adjust the velocity of the tank based on the player's input.
Vector3 velocity = m_Rigidbody.velocity;
velocity += transform.forward * m_MovementInputValue * m_Acceleration;
Vector3 direction = velocity.normalized;
velocity = direction * Mathf.Min(Mathf.Abs(velocity.magnitude), m_maxSpeed);
m_Rigidbody.velocity = velocity;
}
What I do here is add an acceleration value to the velocity each tick depending on whether the input is forwards, backwards, or 0. I then cap this velocity with a max speed. The result is a tank that does not instantly change its speed, letting explosions push it around.
This change drastically changes how the tank handles, however. Increasing the acceleration brings the behavior closer to that of the original, but I have not figured out how to brake the tank (right now it just coasts to a stop)