Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by jakejolli · Dec 05, 2015 at 07:43 PM · physicsrigidbody2dcollision detection

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: alt text

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: alt text

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.

untitled.png (6.0 kB)
untitled2.png (37.2 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jakejolli · Dec 06, 2015 at 10:18 PM 0
Share

Bumpbumpbump

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

39 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges