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 /
avatar image
0
Question by gravelpootis · Apr 09, 2020 at 11:08 PM · simulationtrajectory

Simulating trajectory

Hello, I have tank turret with cannon, when I fire it initialize shell prefab and sets velocity etc. shell has this physics material settings and I want to simulate bouncing too. https://imgur.com/a/9fW2HPi I dont know how to use physics material settings in this code, for boucing Here is the code

     public Rigidbody projectilePrefab;
     public Transform projectileOrigin;

     public LineRenderer lr;

     public int segmentCount = 20;
 
     // Length scale for each segment
     public float segmentScale = 30;
 
     private Collider _hitObject;
     public Collider hitObject { get { return _hitObject; } }
 
     static private Rigidbody projectile;
 
     private void DrawTrajectory()
     {
         Debug.Log($"{projectile.velocity.magnitude}");
 
         Vector3[] segments = new Vector3[segmentCount];
 
         segments[0] = projectileOrigin.position;
 
         // The initial velocity
         Vector3 segVelocity = projectile.velocity;
 
 
         for (int i = 1; i < segmentCount; i++)
         {
             // Time it takes to traverse one segment of length segScale (careful if velocity is zero)
             float segTime = (segVelocity.sqrMagnitude != 0) ? segmentScale / segVelocity.magnitude : 0;
 
             // Add velocity from gravity for this segment's timestep
             segVelocity = segVelocity + Physics.gravity * segTime;
 
             // Check to see if we're going to hit a physics object
             RaycastHit hit;
             if (Physics.Raycast(segments[i - 1], segVelocity, out hit, segmentScale))
             {
                 // remember who we hit
                 _hitObject = hit.collider;
 
                 // set next position to the position where we hit the physics object
                 segments[i] = segments[i - 1] + segVelocity.normalized * hit.distance;
 
                 // correct ending velocity, since we didn't actually travel an entire segment
                 segVelocity = segVelocity - Physics.gravity * (segmentScale - hit.distance) / segVelocity.magnitude;
 
                 // flip the velocity to simulate a bounce
                 segVelocity = Vector3.Reflect(segVelocity, hit.normal);
 
                 /*
                  * Here you could check if the object hit by the Raycast had some property - was 
                  * sticky, would cause the ball to explode, or was another ball in the air for 
                  * instance. You could then end the simulation by setting all further points to 
                  * this last point and then breaking this for loop.
                  */
             }
             // If our raycast hit no objects, then set the next position to the last one plus v*t
             else
             {
                 segments[i] = segments[i - 1] + segVelocity * segTime;
             }
         }
 
         // At the end, apply our simulations to the LineRenderer
 
         // Set the colour of our path to the colour of the next ball
         Color startColor    = Color.red;
         Color endColor        = Color.red;
         startColor.a    = 1;
         endColor.a        = 1;
 
         lr.SetColors(startColor, endColor);
 
         lr.positionCount = segmentCount;
 
         for (int i = 0; i < segmentCount; i++)
         {
             lr.SetPosition(i, segments[i]);
         }
     }
Comment
Add comment · Show 2
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 JxWolfe · Apr 09, 2020 at 11:47 PM 0
Share

sooo, what kind of bouncing are you looking for? Physics materials are easy to work with, and have a bounciness limit, or are you hoping for a cartoonish reflection as in the old wii tank game? alt text

avatar image gravelpootis JxWolfe · Apr 10, 2020 at 10:21 AM 0
Share

At line 50, I want to reflect based on physics material settings, I dont know how

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

124 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Simulating a trajectory that takes collisions into account 2 Answers

how to draw trajectory of rocket for faux gravity in unity???? 0 Answers

Not Able to Convert 2D trajectory(right Side) into 3dTrajectory(forward Direction)... 1 Answer

Create a 3D trail renderer like Hawk eye simulation 1 Answer

Problem with a ballistic trajectory simulation (rigidbody.velocity) 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