Using Unity 2019.4.17f1 and new input system.
I think my implementation is quite straightforward:
The input system calls OnMove which records the movement values:
private void OnMove(Vector2 movement, bool isDown)
{
isMoving = isDown;
if (isDown)
{
nextMove = new Vector3(movement.x, 0, movement.y);
}
}
And Update calls Movement method:
private void Movement()
{
if (isMoving)
{
// Move towards the local Z in global world space
Vector3 motion = transform.forward * nextMove.z;
// Add strafe
motion += transform.right * nextMove.x;
characterController.Move(motion * speed * Time.deltaTime);
}
}
The problem:
When moving the character and firing the weapon, it collides with the projectile.
While it occurs occasionally when playing in the Editor, it is much more frequent in the built executable (in IL2CPP / .NET 4x).
I would expect the same behavior between the 2.
Is it normal ? Is there a way to get more consistent behavior ?
Note: I know I should setup the collision so that the player/projectiles do not collide. But first I want to understand why movement is seemingly faster in build, despite using Time.Deltatime
Player movement code seems framerate independent to me. Unless I’m missing something.
What about your projectile movement code?
1 Like
Thanks for the answer.
Yes, that’s why I came to post the question
What about your projectile movement code?
It is purely physics based, called when the player presses left mouse button.
if (ammoRigidbody != null)
{
originalOrientation = orientation;
ammoRigidbody.velocity = Vector3.zero;
ammoRigidbody.angularVelocity = Vector3.zero;
ammoRigidbody.AddForce(transform.forward * ShootForce, ForceMode.Impulse);
}
Also the weapon handles the fire, the ammo comes from a pool:
GameObject currentBullet = ObjectPoolHandler.Get(bullet);
currentBullet.transform.position = spawnPoint.position;
currentBullet.transform.rotation = spawnPoint.rotation;
currentBullet.transform.forward = directionWithoutSpread;
currentBullet.SetActive(true);
currentBullet.GetComponent<IProjectAble>().Launch(spawnPoint.rotation);
I searched about this problem, I found posts where someone suggests to put the movement in Fixedupdate.
Then movement is a bit slower and more jerky, despite using Time.FixedDeltaTime.
In the doc of Character controller, the example shows the call in update, so I will stick to that.
Well it does not seem to be related to the movement.
I modified the code to make the character move automatically and measure the distance after 10sec.
It is almost the same in Editor and Build:
Editor:
Distance after 10sec (0.0, 0.1, 80.0)
Build:
Distance after 10sec (0.0, 0.1, 80.1)
If I make the character slightly slower, it does not hit the projectile, I will do that or make the projectile spawn a bit farther maybe.