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 ivarhill · Dec 23, 2013 at 09:33 PM · instantiatespeedprojectilespaceship

Issues when instantiating moving object from other moving object

I'm making a prototype of a top-down space RPG in which the player can fire weapons from a controllable spaceship - I've run into some issues with the projectiles fired though. Namely, if I fire (instantiate) a projectile from the spaceship when it stands still everything works, but if I fire it while the spaceship moves it goes significantly slower than it should and stutters quite a bit.

I move the spaceship by adding a force to a rigidbody of the ship model, and I make the projectile (in this case a laser beam) move by simply changing its forward position every second.

Here's the code I use, for the spaceship:

 //Calculate target position
 targetPosition = controlCamera.ScreenToWorldPoint (Input.mousePosition);
             
 //Add force in direction
 rigidbody.AddForce (targetPosition*shipSpeed*Time.deltaTime;);

And for the projectile:

 transform.position = transform.position + transform.forward * speed * Time.deltaTime;

Both in the Update function. I tried using the FixedUpdate function instead for either or both, no difference there.

Any idea why the projectile might be traveling too slow and stutter when fired as the ship moves? It fires straight out from the front of the ship, but significantly faster than the ship's max speed - yet when firing forward on the move it actually moves way slower than the ship.

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 robertbu · Dec 23, 2013 at 09:37 PM 0
Share

You are mixing rigidbody movement (ship) and the movement by direct transform (projectile). This problem would be much easier to solve if the projectile was a rigidbody. You can try having the ship add its speed to the 'speed' variable for the projectile at launch. Assu$$anonymous$$g the ship is always traveling on its forward and the projectile is launched in the the forward direction, you can add rigidbody.velocity.magnitude to the 'speed' variable of the projectile.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by sparkzbarca · Dec 24, 2013 at 02:02 AM

well to start with, when dealing with lasers you should be using raycasting and drawline to just draw a quick planar object between you and the targets, lasers act instantly so there easy to emulate.

If you want to deal with projectile well yes you'll use a rigid body, you'll make its speed on spawn equal to the current speed of the ship + the base projectile speed.

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

Answer by ivarhill · Dec 24, 2013 at 10:23 AM

Thanks for the answers!

This particular laser weapon is not meant to be very realistic - rather a fast traveling projectile as seen in many sci-fi interpretations, so a raycast would not be suitable in this specific scenario.

I'm currently having some issues despite the changes I've made though... the projectiles seem to go slower than they should, definitely far slower than the ship no matter if it moves or not. Of course the mass of the projectile works into it too, but I'll have many different projectiles with different weights, so it might be a bit complicated...

This is my current projectile code (omitting irrelevant parts):

 void Start () {
     
 speed = speed + GameObject.FindGameObjectWithTag("ship").rigidbody.velocity.magnitude;
         
 rigidbody.AddForce(transform.forward * speed* Time.deltaTime);
 }
Comment
Add comment · 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

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

19 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

Related Questions

Tank projectile speed 2 Answers

3D Space Shooter Firing Projectiles 0 Answers

How can you instantiate a prefab with an initial burst of speed using ForceMode2D.Impulse? 2 Answers

Click button to fire weapon. 3 Answers

Having problems with Instantiate. 2 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