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 MaskedPixel · Jul 15, 2013 at 04:20 PM · instantiateprefabaddforce

AddForce on Prefab has inconsistent behaviour.

I am instantiating a prefab and using AddForce via ForceMode.Impulse to make it move. Unfortunately, every once in a while (approximately 1 in 20), AddForce does not work. Here is the relavent code:

 using UnityEngine;
 using System;
 using System.Collections;
 
 [RequireComponent(typeof(PauseManager))]
 public class PlayerConstructor : MonoBehaviour
 {
     public GameObject playerPrefab;
     public GameObject planetLooperPrefab;
     
     public GameObject player;
     
     public Transform startPosition;
     public Transform impulseTarget;
     public bool planetLooper;
     
     void Awake()
     {
         player = (GameObject)Instantiate(playerPrefab);
         player.name = "Player";
         
         GetComponent<PauseManager>().toPause.Add(player.GetComponent<PauseBehaviour>());
         
         if(startPosition != null)
             player.transform.position = startPosition.position;
         
         if(startPosition != null && impulseTarget != null)
             player.rigidbody.AddForce(impulseTarget.position - startPosition.position, ForceMode.Impulse);
         
         if(planetLooper && planetLooperPrefab != null)
         {
             GameObject looper = (GameObject)Instantiate(planetLooperPrefab);
             looper.transform.parent = player.transform;
             looper.GetComponent<PlayerLooped>().player = player;
             looper.GetComponent<MessageBroadcaster>().targets[0] = gameObject;
         }
     }
 }

So far in my troubleshooting I have confirmed:

  • startPosition and impulseTarget are not null.

  • AddForce does indeed get called.

  • The force being added is significant. (it is 1000 on a game object of mass 1)

  • The object is not colliding with anything (Debug.Log in OnCollisionEnter)

Comment
Add comment · Show 3
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 MaskedPixel · Jul 15, 2013 at 04:51 PM 0
Share

No dice. I double-checked via Debug statements and breakpoints. AddForce is being called and a significant force is being applied. No collision.

avatar image robertbu · Jul 15, 2013 at 04:57 PM 0
Share

I should have read your question more closely given your list at the bottom. You do say that the force being applied is significant. Does that mean you output impulseTarget - startPosition.position? As a check you could try:

 player.rigidbody.AddForce((impulseTarget.position - startPosition.position).normalized * 1000.0, Force$$anonymous$$ode.Impulse);
avatar image MaskedPixel · Jul 15, 2013 at 05:14 PM 0
Share

Same behaviour. I'm now looking at: http://docs.unity3d.com/Documentation/$$anonymous$$anual/ExecutionOrder.html

At the bottom, it says physics simulation occurs before rigidbody applies transform and position. $$anonymous$$aybe there is a problem with the force I am applying being cleared if there are multiple physics simulation call(which could easily happen as my script is executing at scene load, and that could create a larger deltaTime). I'm trying to figure out how to test this now.

1 Reply

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

Answer by MaskedPixel · Jul 15, 2013 at 06:16 PM

I have since moved that AddForce that was causing issues from 'Awake' to 'FixedUpdate'. The problem seems to have disappeared.

This solution confuses me because if you check the docs on Execution Order, Awake is called before FixedUpdate and there is nothing specifying that anything in the Physics changes between this. The Rigidbody docs do specify that all physics functionality should be used within FixedUpdate, but I still don't see why it couldn't be used in Awake.

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

15 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

Related Questions

Object after instantiate doesn't apply AddForce 1 Answer

AddForce not working on instantiated bullets 1 Answer

Add force to instantiated prefabs (c#) 0 Answers

Spawning a prefab at another object's location 3 Answers

Instantiate individual components of a prefab? 1 Answer


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