I have a projectile that have a target location. At the moment I use
transform.position = Vector3.Lerp (transform.position, _target.transform.FindChild("Champion").transform.FindChild("Heart").transform.position , _projectileSpeed * Time.deltaTime);
and a OnCollisionEnter to determine if the projectile hits an obstacle or the target.
The problem I´m having is that the movement of the projectile gets shaky at the end and sometimes travles around its target (cause of the rigidbody or such?). I´m looking for a better way to shoot the projectile towards the target in a strait line and at the same time look what the projectile hits on the way. The projectile has to be visible for other players etc.
[SOLUTION]
I found that by changing “Is Kinematic” (rigidbody) to true on my projectile and using
void OnTriggerEnter(Collider other)
to be the most effective way to shoot it. I´m still using
transform.position = Vector3.Lerp (transform.position, _target.transform.FindChild("Champion").transform.FindChild("Heart").transform.position , _projectileSpeed * Time.deltaTime);
to send the projetile at it’s location, but since I changed “Is Kinematic” it flows freely and doesn´t bump into other objects. And since I use OnTriggerEnter i can check for enemies, obstalces etc and destroy the projectile if needed.
I dont know what you are trying to achieve - but your code lets the projectile follow the target even if it moves (assuming that Champion is a character or enemy).
In genereal a projectile like a bullet will not change its direction after leaving the barrel (only affected by gravity and wind - but i dont think that you need this).
Physics are from my experience very bad to track projectile collision because they are often too fast and tiny. Often colliders pass other colliders in between the Physics steps.
My advice would be to look at raycasting.
Raycast every update in transform.forward direction to check if theres any collider in front of the projectile then move it forward based on the desired speed (speed * time.deltatime) .
If you want to let the projectile follow the player/enemy - just rotate it towards the target every update.
Other physics based tutorials are in the Unity example projects (at least for a rocket type projectile).
First, Lerp() used this way will give you a eased movement towards the end…not what you want in a projectile. Second, Lerp() transports object from one position to another. The result can be a conflict between collisions and the transport or often the collision is missed entirely. I’m not an expert in this area, but try this:
var pos = Vector3.MoveTowards(transform.position, _target.transform.FindChild("Champion").transform.FindChild("Heart").transform.position , _projectileSpeed * Time.deltaTime);
rigidbody.MovePosition(pos);
A couple of other things. I believe Transform.FindChild() is depreciated. It was replaced by Transform.Find(). And you can use a path in Find(). So I believe you can do:
_target.transform.Find("Champion/Heart");
You might also consider doing this calculation only once when the target is acquired rather than in the Update() loop.
[SOLUTION]
I found that by changing “Is Kinematic” (rigidbody) to true on my projectile and using
void OnTriggerEnter(Collider other)
to be the most effective way to shoot it. I´m still using
transform.position = Vector3.Lerp (transform.position, _target.transform.FindChild("Champion").transform.FindChild("Heart").transform.position , _projectileSpeed * Time.deltaTime);
to send the projetile at it’s location, but since I changed “Is Kinematic” it flows freely and doesn´t bump into other objects. And since I use OnTriggerEnter i can check for enemies, obstalces etc and destroy the projectile if needed.