First off, brand new to Unity. It's awesome and so much fun to play around with, but I'm so lost.
Ok, so, I have a top-down, 2-d shooter where I have a character (sphere, for now) that shoots smaller spheres (bullets.)
The game is in the x,z plane, as in the y isn't needed because it's a 2-d game. Now I've been looking for a way to be able to add force to my instantiated bullets, so they travel TOWARD my mouse position. I've been doin' a little searching and doctoring and stuff, but I fear I've dug myself into a hole. Now my script looks like this:
var shootForce:float = 500;
function Update ()
var myPos = transform.position;
var dist = Camera.main.transform.position.y - myPos.y;
var x = Input.mousePosition.x;
var z = Input.mousePosition.z;
var dir = Camera.main.ScreenToWorldPoint(Vector3(x, dist, z)) - myPos; var BulletClone = Instantiate(prefabBullet, myPos, Quaternion.LookRotation(dir));
BulletClone.rigidbody.velocity = dir.normalized * shootForce;
Destroy(BulletClone,5); //<-- ignore this, i'll fix it later. focus on the top part
Now the problem is, when I click ("Fire1"), the prefabs instantiate and then just chill in space. They don't have any force added to them or launch in any way. Both them and my player gameobject have rigidbodies. Use Gravity is checked off on the bullet prefab. The only thing I could think of is if they get caught in the player gameobject's rigidbody and then just chill in space. If this is the case then how would I fix that? Instantiate it TOWARD mouse position before adding force toward mouse position? I have a feeling i'm on the right track (but maybe not).
Thanks in advance for your help! :D
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
2 People are following this question.
Creating a wrap around bullet effect like Asteroids.
Help with topdown movement in Unity2d?
How to make a 2D character points his gun to the mouse position?
2d camera help!