- Home /
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.
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?
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?
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
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));