• 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 Rxanadu · Oct 19, 2012 at 01:19 PM · rigidbodyinstantiateprefabs

Moving a rigidbody prefab forward wafter instantiated

I've been following this tutorial, and, after 2 weeks of plugging away at it, I'm still not able to launch the cube at the wall without creating numerous cubes that bounce off each other to hit the wall by chance.

Here's the code I'm using for this process:

 using UnityEngine;
 using System.Collections;
 
 public class LaunchObject : MonoBehaviour {
 
     public GameObject CubePrefab;
     
     void Start () {
 
     }
     
     void Update () {
         if (Input.GetKeyUp(KeyCode.Space))
         {
             Debug.Log("Instance created");
             Instantiate(CubePrefab, transform.position, transform.rotation);
             var fwr = transform.forward * 50;
             CubePrefab.rigidbody.AddForce(new Vector3(0,0,10.0f));
         }
 
     }
 
     void OnCollisionEnter(Collision theCollision)
     {
         //checks for prefab clone's existence
         if (GameObject.FindGameObjectWithTag("Cube"))
         {Debug.Log("Hello, Cube");
             if (theCollision.gameObject.name == "Wall")
             {
                 
                 Destroy(GameObject.Find("Wall"));                
             }
         }
     }
 }
 

The main issue I have here is with the Update() function: I can't get the AddForce() function call to work (i.e. the cube does not move forward on the z-axis). I've put it in the Start() function, and, as I expected, nothing happened there, either.

I'm just tired of this right now, so any help would be appreciated. It seems too silly to be stuck on this sort of material, but here I am.

And, yes, I've contacted the "teacher" and sent a copy of my script to him to look over. I've yet to hear from him about it since yesterday.

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

3 Replies

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

Answer by Rxanadu · Oct 19, 2012 at 05:43 PM

None of the suggestions worked.

I'm getting tired of Unity, but I need it for a community project. Is there anyway to just post the entire project here?

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 madmike6537 · Oct 19, 2012 at 02:12 PM

Ill take a guess but I am probably wrong. In the unity script reference, it says if you are using itside void(update) you need to apply an amount of force: for example:

  void Update() {        rigidbody.AddForce(Vector3.up * 10);

Or, you could put it in a fixedUpdate and it doesnt look like you need the multiplyer at that point:

 void FixedUpdate() {        rigidbody.AddForce(0, 10, 0);    }}

So - maybe try using fixedupdate instead of update and see if that works?

Comment
Add comment · Show 1 · 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 lil_billy · Oct 19, 2012 at 02:21 PM 0
Share

he was trying to add initial force from the object instantiating the prefab though yes you could just put a script on the prefab that tells it to move after being spawned

avatar image
0

Answer by lil_billy · Oct 19, 2012 at 02:15 PM

try this

var something =Instantiate(CubePrefab, transform.position, transform.rotation); something.rigidbody.AddForce(new Vector3(0,0,10.0f));

you man need to find the rigidbody component first something.GetComponent(rigidbody).AddForce(new Vector3(0,0,10.0f));

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Rigidbodies violently repel when replacing with pre-shattered opject 2 Answers

Instantiated prefab doesn't collide with player 0 Answers

Instantiated Bullet Force not being applied 0 Answers

How can I instantiate two prefabs at different times and positions ? 1 Answer

Instantiate Pre-fab clone altered on 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