Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 PacmanBits · Jan 28, 2013 at 08:45 PM · collisionphysicsinstantiateprojectile

Giving Instantiated Object Velocity Causes Wrong Position

I'm knocking out a super simple projectile script. I instantiate an object at a position, no problem. I instantiate an object, on the next line give it a velocity, all of a sudden the object starts at the center of the object that instantiated it.

I use the following JS:

 var bullet : GameObject;
 var muzzleVelocity : float = 10.0;
 private var aim : Vector3 = Vector3.right;
 
 function Update()
 {
     if(Input.GetKeyDown("space"))
     {
         var b : GameObject = Instantiate(bullet, transform.position + aim * 2, Quaternion.identity);
         
         b.rigidbody.velocity = aim * muzzleVelocity;
     }
 }

Commenting out the line where the velocity is set results in the bullet getting spawned at the correct location (in this case, 2 units to the right).

EDIT: It looks like the physics engine isn't revving up in time. I've included the following on the bullet:

 var colSparks : Transform;
 
 private var last : Vector3 = Vector3.zero;
 private var del : boolean = false;
 
 function Start()
 {
     last = this.transform.position;
 }
 
 function Update()
 {
     if(del)
     {
         Destroy(this.gameObject);
     }else{
         var dir : Vector3 = last - this.transform.position;
         last = this.transform.position;
         Debug.DrawRay(this.transform.position, dir, Color.green);
         
         var hit : RaycastHit;
         
         if(Physics.Raycast(this.transform.position, dir, hit))
         {
             Debug.Log(hit.collider.name);
             Instantiate(colSparks, hit.point, Quaternion.LookRotation(hit.normal));
             
             if(hit.collider.rigidbody)
             {
                 Destroy(this.rigidbody);
             }
             this.transform.position = hit.point;
             
             
             del = true;
         }
     }
 }

I'm guessing the instantiation happens, and Start() runs on the above script and sets last before the instantiating function continues and sets the new position. When the first update runs and finds the new position, it checks for collisions along that line and finds the original instantiating object.

New question: can anyone think of a good cheap, simple fix for this?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Doireth · Jan 28, 2013 at 08:49 PM

Instead of altering the velocity (which you should generally never do as the physics engine will perform those calculations) you should use AddForce(). For example:

 b.rigidbody.AddForce(aim * muzzleVelocity);

http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html

Comment
Add comment · Show 9 · Share
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 PacmanBits · Jan 28, 2013 at 08:54 PM 0
Share

I had actually seen someone suggest that in a similar question. It didn't work but I DID mean to use that version of my code. Anyway, I think I've figured out the problem, but I've raised a new, more general question above.

avatar image Doireth · Jan 28, 2013 at 09:00 PM 0
Share

For your edited question (though in future it's best to make a new question) you should use OnCollisionEnter() (see http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html). For example:

 function OnCollisionEnter(collisionInfo : Collision)
 {
     Destroy(gameobject);
 }

As the documentation notes, if you are not using the collision parameter you can leave it out as it reduces calculations.

avatar image PacmanBits · Jan 28, 2013 at 09:03 PM 0
Share

By using OnCollisionEnter, though, I'm losing the ability to raycast between frames to check for "paper walls", aren't I? Ballistic projectiles aren't going to be very common, so I'm okay taking the time and processing power to make sure they're handled well.

Shall I make a new question, still? Thanks for all of your patience.

avatar image PacmanBits · Jan 28, 2013 at 09:06 PM 0
Share

Eh, I just added a function jump() to the second script that updates variables (such as last) in the case of a sudden jump where collisions shouldn't be checked. It's a little hacky, but it works. Thanks for all the help, selecting your answer now.

avatar image Doireth · Jan 28, 2013 at 09:06 PM 0
Share

A combination of both would work nicely. Since projectiles are not to be very common then the extra processing of both to make sure it's done right would be perfectly fine. Neither have any major performance costs you need to worry about.

Show more comments

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

10 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

Related Questions

Resume movement of instanced object relative to the original after instantiation. 2 Answers

PhysX and multiple instances at same location 1 Answer

Innacurate terrain collision with fast rigidbody 1 Answer

Projectiles, their speed, and collisions 1 Answer

Bullet projectiles with collision info without affecting others 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