- Home /
Trouble shooting cube along in a straight line
Hey guys! I've been a pretty long time C# programmer and a moderate amount of time spent with js, but am pretty new to Unity so am hoping for some help, I'm currently attempting to create a sort of multiplayer online arena type of game where players will face off against each other in a FFA format so I've got my Plane, PlayerCube and EnemyCubes, HP bars and all that jazz but I'm running into a bit of a problem when it comes to projectiles, here's the code I tried after my first attempt failed:
function Update () {
// Put this in your update function
if (Input.GetKeyDown(KeyCode.Space)) {
// Instantiate the projectile at the position and rotation of this transform
var clone : Transform;
clone = Instantiate(projectile, transform.position, transform.rotation);
// Add force to the cloned object in the object's forward direction
clone.rigidbody.AddForce(clone.transform.forward * shootForce);
}
}
so when I run this piece of code my PlayerCube is able to shoot cubes but they actually fall through the floor after about half a second and I need them to be able to travel in a straight line (without falling through the ground or accelerating upwards).
I usually like to work through these things on my own but this has had me stumped for about an hour now, I tried doing "clone.rigidbody.velocity" to make the cube come out a tad bit more upwards but then the cube eventually goes flying into the air later in the screen, I want people to be able to hit other players who are on the plane on the other side of the screen so it needs to really travel along the same axis without falling or going higher.
Would really appreciate any help with this! Also I've been trying to destroy the projectile cubes when they hit an enemy cube with:
function OnCollisionEnter(theCollision : Collision)
{
if(theCollision.gameObject.tag=="Bullet")
{
Destroy(gameObject);
}
}
but it doesn't seem to work for me, I have the trigger ticked on the enemy cube but as usual the projectile cubes just pass straight through it and keep travelling.
Thanks in advance for the help!
Simple question, did you uncheck the gravity parameter of your prefab rigidbody ?
oh wow, never actually expected something this simple haha, over complicating seems to be the source of my problem yet again!
Thanks very much for the tip, seems I have a lot more to learn about unity, now I just have to figure out why my projectile cubes aren't colliding correctly with the enemy cubes.
Also I have no idea how to mark your comment as a correct answer or if I'm even supposed to?
Answer by KiraSensei · Oct 05, 2012 at 10:24 PM
This should help you with your remaining problem (I asked this kind of question before and had a lot of trouble correcting the problem ...) :
that helped a lot! thanks very much, I have everything fixed now :)
Answer by nikescar · Oct 05, 2012 at 10:25 PM
Set the Rigidbody constraints of your bullet prefab from the inspector. You can freeze the position and rotation of any axis.
Your answer
Follow this Question
Related Questions
How to make new Axis? 2 Answers
Rotate cube in two direction not working 1 Answer
Why won't my crate fire? 3 Answers
How i can create a cube with specific coordenates? 3 Answers
Flipped objects Rotation issues 1 Answer