Projectile Changing Angle BEFORE Impact
I have a projectile with a Trail Renderer. When this projectile hits an object, I want to essentially make it inactive, but I can't destroy it right away, as the trail renderer will end abruptly and it doesn't look right.
Effectively, I would like the trailrenderer to reach the impact position, and then destroy the projectile.
I'm having an issue where my projectile is changing angle slightly just before it hits a collider. I don't want this, I just want it to stop at the point of impact, allowing the trail renderer to 'catch up'
This doesn't seem that bad in the screenshots below, but it's much more noticeable during gameplay.
The collider for the road is perfectly in line with the graphic.
Here's what's happening:
And here is my code: For the object which spawns the projectile
public class WeaponController : MonoBehaviour {
public GameObject bullet;
private GameObject newBullet;
public float bulletSpeed;
public float delayBase;
private float delay;
private float accuracyMod;
public float accuracyModMax;
public float accuracyModMin;
void Start() {
delay = delayBase;
}
void Update() {
//Handle rotation of arm and weapon
Vector2 target = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
Vector2 myPos = new Vector2(transform.position.x, transform.position.y);
Vector2 direction = target - myPos;
direction.Normalize();
Quaternion rotation = Quaternion.Euler(0, 0, Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg);
transform.rotation = rotation;
Vector3 scaleTemp = transform.localScale;
if (transform.eulerAngles.z > 270 || transform.eulerAngles.z < 90) {
scaleTemp.y = 1;
} else {
scaleTemp.y = -1;
}
transform.localScale = scaleTemp;
//Determine accuracy of the projectile
accuracyMod = Random.Range(accuracyModMin, accuracyModMax);
//Determine Projectile velocity
Vector2 projectileTarget = new Vector2(target.x, target.y + accuracyMod);
Vector2 projectileDirection = projectileTarget - myPos;
projectileDirection.Normalize();
if(newBullet != null) {
// newBullet.transform.position = GameObject.Find("Barrel").transform.position;
newBullet.GetComponent<Rigidbody2D>().AddForce(projectileDirection * bulletSpeed);
newBullet = null;
}
if (Input.GetMouseButton(0) && delay <= 0) {
newBullet = (GameObject)Instantiate(bullet, GameObject.Find("Barrel").transform.position, transform.rotation);
delay = delayBase;
} else {
delay -= Time.deltaTime;
}
}
}
For the projectile
public class Projectile : MonoBehaviour {
void OnCollisionEnter2D(Collision2D col) {
GetComponent<CircleCollider2D>().enabled = false;
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
GetComponent<MeshRenderer>().enabled = false;
}
}
This code will do more in the future, but I'm just trying to get the visuals right at the moment.
Also, here is a screen of the projectile's inspector:
It also has a Sphere mesh filter, a Mesh Renderer and the Projectile Script attached.
I set the RigidBody's Interpolate to Interpolate, as this seems to minimize the 'Angle on impact' effect. I also tried the other two options, but they made it worse.
Your answer
Follow this Question
Related Questions
How to stop game object movement instantly after collision is detected ? 1 Answer
Restricting Rotation Of an Object in VR 1 Answer
[UNET] Rigidbody physics sync 0 Answers
Oscillate object with forces/velocity 2 Answers
Unity : How to handle compound colliders 2D physics collision detection 0 Answers