- Home /
I would like to make objects spawn and aim for a object that i have to dodge
i have been making my game and i the aim of the game is the player is on a surface just above the water and there are cubes spawning from a distance in front of you and you have to dodge them. i was wondering if any of you guys would be able to give me a script that might help me in making this game. the objects spawning could either be a cube or a sphere. please if any of you guys could help me it would be great thanks
Answer by robertbu · Feb 14, 2013 at 12:42 AM
Here's something to get you started. You may want to change it so that it aims a bit above the target to compensate for gravity.
public class DodgeBallCreator : MonoBehaviour {
public GameObject goTarget; // Where to target
void Start() {
InvokeRepeating ("ThrowBall", 0.0f, 2.0f);
}
void ThrowBall() {
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
go.transform.position = transform.position;
go.AddComponent<Rigidbody>();
go.transform.LookAt(goTarget.transform.position);
go.rigidbody.AddRelativeForce(Vector3.forward*1000);
}
}
Your answer
Follow this Question
Related Questions
Spawn object near the moving camera 1 Answer
Code based 3d multiple cube instantiation 1 Answer
Character Loading Location 2 Answers
Mouse Click and Spawn object 4 Answers
Spawn something 1 Answer