|
Hello everyone. My friend is helping me make a sidescrolling 2d shooter that runs off of the y and x axis. He doesn't use unity so he wrote some script down and I was wondering if someone could help me convert it to unity javascript and also tell me if it would work for shooting to the mouse. Here is his code: mPos = vector3; (mouse position) pPos = vector3; (player position) bPos = vector3; (Bullet position) xdir = mPos.x - pPos.x; ydir = mPos.y - pPos.y; angle = Atan2(xdir,ydir); lx = cos(angle); ly = sin(angle); bPos.x += bulletMoveSpeed * lx * Time.deltaTime; bPos.y += bulletMoveSpeed * ly * Time.deltaTime; I hope this code will work because shooting in 2d has given me countless problems.
(comments are locked)
|
|
You need to put all that inside a function called Update(). Here is some things you'll need. Input.mousePosition, Camera.ScreenPointTo, Transform.position, Mathf.Atan. Thank you. That helps a lot more.
May 15 '12 at 08:00 PM
timo0060
I'm trying to get the position of my player (pPos), but I can't get it. I've tried setting up the player as a rigidbody in my script and taking the player.x from it, but I can't add my player as a rigidbody. Any other suggestions on how to get the x and y position of my player?
May 15 '12 at 10:48 PM
timo0060
To get the player's position, you a reference of its gameobject or of any of it's component to access its Transform, where the position is stored. The whole matrix actually. Anyway, it depends on what you script is attached to. If it's the player, you can have the position with transform.position. If not, you can either declare the var outside the functions then setting it up in the inspector, or you can use Find functions, preferable FindWithTag (the fastest)
May 16 '12 at 02:57 PM
Berenger
Thank you. I decided to just use static variables in my player script to get the x and y positions and then just call them in my Bullet script. My only problem now is the bullet won't move. I modified the code to look like this: transform.Translate.x(bulletMoveSpeed * lx * Time.deltaTime); transform.Translate.y(bulletMoveSpeed * ly * Time.deltaTime); Any ideas on how to make the bullets move? They just sit there after being spawned. Thanks for your help so far. This community is a good one.
May 16 '12 at 03:18 PM
timo0060
Translate is a function, it doesn't have members. You can't call Translate.x(...). Instead, call transform.Translate( Vector3( xstuff, ystuff, 0 ) ). If it's C#, add new before Vector3. And make sure none of those variable is 0, that the bullet's position isn't modified elsewhere and that the function is actually called. It should be in Update().
May 16 '12 at 03:39 PM
Berenger
(comments are locked)
|
|
This is not a translation of you code but it is code that should work... I am rather bored and should be revising so this was a more entertaining alternative. Hopefully it works though I haven't tested it and as I don't know how your game is set up I don't know if it will be relevant but here you go: Scribe This doesn't do anything that my code was supposed to do. My code was to move my bullets, your's are shoot my bullet. But your code helps a lot because it'll allow me to shoot easier. Thank you very much for this lovely piece of code which is going to replace my current script. Cheers Mate
May 16 '12 at 06:24 PM
timo0060
There's just one problem with your code. When I fire, it's a continuous stream instead of the 1 every 0.5 seconds that the rate of fire is set up to. Any idea how to fix that?
May 16 '12 at 07:11 PM
timo0060
sorry for the late reply, It does that because I was being a bit stupid, the if statement in the update should probably be... Hopefully that works and glad it helps despite being unrelated to your question! sorry bout that
May 29 '12 at 01:19 PM
Scribe
Thank you. It's better late then never, and I did end up getting it working. Thank you for all the help you gave.
May 29 '12 at 01:51 PM
timo0060
(comments are locked)
|

That edit you does nothing. Next time code you please help me out more? Shooting is stressing me out, yet it's so simple of a concept.
I got it working, had to put Mathf. in front of Atan2, sin, and cos. But the bullets don't move at all, why won't they move?
@timo0060, this edition was not intended to solve your problem: it just formatted your code. And it helped you - many guys here just refuse to read badly formatted questions.
Anyway, the code above or any variations on the same theme will not work in Unity: it's a full 3D engine, even when powering 2D games. You must think in terms of 3D coordinates, not in 2D screen space.
He could have just deleted your entire post instead of using up a minute or two of his lifespan to format a code the writer of which wasn't concerned with the presentation of and didn't care whether or not it was hard to read etc for the people said code writer was asking help from.