- Home /
Intercepting Alghorithm not wok properly
Try to Intercept Object A with Object B, when Calculating Arrive-time and Probable Location in Object A I show something to Know calculation data. But calculate position is not work properly best to see the Gif Below (Green One is object A and Red in Object B, the Grey is Calculated position that object A should look and shoot) (As see the Gray Object is Not go Smoothly it's get back to Red Position repeatedly):
This is the Code for Intercept:
public GameObject Enemy;
public GameObject GhostObject;//Grey Object That Show Calculating Position
Rigidbody enemyRig;
Transform enemyPos;
Vector3 bulletSpeed = Vector3.forward * 0.08f;
Vector3 current;
Vector3 last;
float time;
Vector3 enemyVel;
// Use this for initialization
void Start ()
{
enemyPos = Enemy.transform;
enemyRig = Enemy.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update ()
{
enemyVel = CalEnemyVel();
Vector3 targetPos = enemyPos.position;
Vector3 Vr = enemyVel - transform.forward * 10f;
Vector3 Sr = targetPos - transform.position;
time = Sr.magnitude / Vr.magnitude;
Vector3 lookpos = targetPos + (enemyVel * time);
transform.LookAt(lookpos);
GhostObject.transform.position = lookpos;
}
Vector3 CalEnemyVel()
{
Vector3 vel;
current = enemyPos.position;
if (last != null)
{
vel = ((current - last) / Time.deltaTime);
}
else vel = Vector3.zero;
last = current;
return vel;
}
Movement Method for Red
public float speed = 0.5f;
void Update ()
{
float h = Input.GetAxis("Horizontal");
//GetComponent<Rigidbody>().MovePosition(transform.position + Vector3.left * h * speed);
transform.Translate(Vector3.left * h * speed);
}
I would say that there are frames where in CalEnemyVel last and current are the same value and turn zero. I don't know how you move, but most likely not as often as this is called
I try Two method first move with rigidbody.$$anonymous$$ovePosition and then transform.translate. getting velocity directly from rigid body cause weird result so I calculate velocity myself. now with both this thing happens. I adding movement method.
Answer by FoodLover195 · Oct 21, 2018 at 03:24 AM
Hi @FDMpro
Although I didn't read through most of your code (not very clean to read), from my understanding the green object has to shoot infront of the red object because you have calculated the interception point using the bullet speed, enemy speed, and enemy direction. If that's the case are you sure it's not working? From the gif you attached it would seem that the ghost object was calculating correctly. Assuming I'm not missing anything, the results you have would make sense because as the red character turns, it's velocity or rather Input.GetAxis slows down. This obviously means the red object isn't going to travel as far, therefore the ghost retracts slightly. This continues until eventually the force isn't cancelling itself and it starts moving in the opposite direction. I hope that makes sense, just tag me if you have anymore questions ^_^
Thanks for reply. at first sorry about my bad English . as you say the green object should look And shoot at red object. but the bullet is not ray-cast (here I don't show bullet) it's have a speed and I have to calculate ai$$anonymous$$g position for bullet to hit object. the grey object position show where calculated result is pointing. the problem comes now is if you look at gif the gray object in one frame is at calculated position and in some frame is in red position (when red is moving) this most not to happen because the red object is moving continuously . as @hexagonius say, maybe in some point velocity is become zero or something is become zero so grey not go to desire location but I can't find it.
Yes exactly. It's the Input.GetAxis function. Sorry I tried to explain it but apparently not well enough. You can trust me when I say the code is working 100% (At least seems so from my understanding of the movement). The issue is your desired outcome may not align with the actual outcome. I'l try and show whats going on:
Controls relative to the h variable which is using Input.GetAxis
Press Right - h goes up
Press Left - h goes down
There for imagine this:
Player holds down right. h goes from 0 to 1
Then player holds down left. h goes from 1 to -1
If you relate that back to your ghost object, when h is 1, the object is 1 unit infront of your enemy. Then as you click left to make h -1, it needs to pass back through the enemy (at h = 0) to get to the left side when h is -1.
Does that make sense?
O$$anonymous$$ I update the Gif so maybe shows it better and Yes your right the way of axis working is through 0 to 1 but if you look the stuttering is happens even when I try to go same direction for a long time. And other thing if the Axis go through 0 to 1 it's not have to go back it's go slowly until reach full speed but now it's go back and forward while Red Just move in constant speed(h = 1).
Oh, okay. I didn't think the jittering was such a big deal provided it stays within certain bounds, but I can see how that would be an issue. I've tried to analyse your code but somethings don't make sense to me.
Firstly, I noticed you are using a variable called enemyPos in CalEnemyVel() however you only set the enemy pos in the start function? If so wouldn't this affect the calculations of a moving enemy if you use a stationary vector? Also is the purpose of vr and sr?
At this point I'm also just gonna build my own little test project and see if I can come up with a solution :)
Your answer
Follow this Question
Related Questions
how do i make this lag less? 1 Answer
Resetting rigidbodies' transform values 1 Answer
Animation or smooth teleportation 0 Answers