Adding In PlayerMovement to Bullet Trajectory

Hi there,
I added a delta positioning system to my players. I use this to determine the movement of my player, his momentum and apply that to bullets fired to create a more realistic shot. Meaning, my player moves around quite a bit, and by adding the delta position to the bullets trajectory, the bullet should fire in the direction of the barrel, AND the movement of the player.

This is all fine and dandy but I have noticed a glitch in that perhaps what I am adding to the guns natural trajectory is too drastic. Meaning my bullets generally go in the right direction but also seem to spread out, and go off in unexpected angles too often.

I am wondering if somebody can help me.

I gain delta position information by recording player position at start the comparing it to current position in Update.

function Update()
{
deltaPos = starPos - crntPos;
}

I then apply the delta values to my trajectory.

function fireGun()
{
Trajectory = Vector2(run + deltaPos.x , rise + deltaPs.y);
Trajectory.Normalise();
}

Can anyone see if I could be doing something different to become more accurate?

Thanks.

I believe your update function will need to do this instead:

deltaPos = lastPos-crntPos;
lastPos=crntPos;

That was your delta is always reseting and it doesn’t get drastically bigger as you move further away from your starting position (may explain the massive trajectory).

My apologies, it was, is. I just came up with the code, but it is strange. I had a thought to normalize the delta vector, then add it to the normalized trajectory victory, then normalize that vector. A triple normalization if you will. Anyhow, it’s 5 in the morning so, will test later.

Cheers. If you have any other ideas, please add.

Thanks.
Ren

This seems to work for me, I just attached it to a cube and moved it a round in the editor.

I think you need to clamp the magnitude of the vector you use to add to the direction.

using UnityEngine;
using System.Collections;

public class NewBehaviourScript1 : MonoBehaviour {

	Vector3 lastPosition;
	Vector3 currentPosition;
	Vector3 deltaMovement;
	
	float maxMagnitude = 1;
	
	// Update is called once per frame
	void FixedUpdate () 
	{
		
		currentPosition = this.transform.position;
		deltaMovement = lastPosition-currentPosition;
		lastPosition = currentPosition;
		
		Debug.DrawRay(this.transform.position, this.transform.forward, Color.white, 3);
		Debug.DrawRay(this.transform.position, deltaMovement.normalized, Color.red, 3);
		Debug.DrawRay(this.transform.position, Vector3.ClampMagnitude(this.transform.forward + deltaMovement,maxMagnitude), Color.green, 3);
	
	}
	
}

Thanks.
To move my player I am using iTweenPath. With this system, the result is a smooth flow of movement over a path. However, distance between nodes plays a role in momentum. If close, object moves slower than if far apart. So, while 99% smooth, my thinking is occasionally a small “jerk” can occur, this may be throwing off the odd bullet.

I looked into ClampMagatude but cant see how to apply it to my situation, and what the result would be.

Well you have your bullet direction and the direction you want to add to it, basically you make sure you clamp the direction you want to add to make sure it doesn’t exceeed a certain magnitude (keeps the small adjustments but gets rid of the big ones).

Right, so clamping does what exactly? Sets a limit?

My one concern is that I have the two vectors. Both should be normalized, then added and normalized. Regardless. I think the cause is any slight jerky movement made. Which is not often. I am wondering how the clamp could help this. As my firing can be random. My player can aim in any 360 degrees at any time.

The idea is to set the limit on how much extra movement the trajectory can have, the clamp will do that. If the direction was calulated without clamping to be going +2 in the up vector and the magnitude was set to +1 then the up vector can only ever be +1.

ok. the issue I see with that is that my y delta can vary drastically over time. So, since the delta y is the issue, how can I get a more consistent feel? Meaning, my player, over 1 second may go from moving at y delta = 0.1 to y delta = 0.2 then back to 0.1.

Any bullet fired over that time frame while y = 0.1, will move accordingly, while any bullet fired while y = 0.2 will be falling (or rising) at double that, creating a not so desirably effect visually. It may be real world math but if I could some how clamp that. Can you see how or if Clamp is suitable for this? If so, could you write a snippet of code, as I can not see how.

Thanks.

Basically, you take the vector3 that you are adding to your trajectory and you this:

yourVector3 = Vector3.ClampMagnitude(yourVector3,0.05f);

Replace 0.05f with a bigger or smaller number depending on how it plays.

Great. Will try. This limits the vector to 0.5 or whatever I set it to?

The clamp, seems to be working.

Thank you.