- Home /
Code Converstion into javascript Help?
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.
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 $$anonymous$$athf. 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 ins$$anonymous$$d of using up a $$anonymous$$ute 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.
Answer by Berenger · May 15, 2012 at 07:52 PM
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.
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?
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)
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. $$anonymous$$y only problem now is the bullet won't move. I modified the code to look like this:
transform.Translate.x(bullet$$anonymous$$oveSpeed lx Time.deltaTime); transform.Translate.y(bullet$$anonymous$$oveSpeed 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.
Translate is a function, it doesn't have members. You can't call Translate.x(...). Ins$$anonymous$$d, 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().
Answer by Scribe · May 16, 2012 at 04:16 PM
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:
var GunObj : Transform; //the gun
var MouseWorldPos : Vector3; //a variable to store position of cursor
var Bullet : GameObject; //bullet/projectile to be fired
var ROF : float = 0.5; //Rate Of Fire of your gun
function Update() {
MouseWorldPos = camera.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, GunObj.position.z));
GunObj.LookAt(MouseWorldPos);
if(Input.GetButton ("Fire1")){
InvokeRepeating("Shoot", 0/*start firing imediately (no delay)*/, ROF); //start calling the Shoot function
}else{
CancelInvoke("Shoot");
}
}
function Shoot() {
Instantiate(Bullet, GunObj.position, GunObj.rotation); //creates a new 'bullet' every time this function is called
}
Scribe
This doesn't do anything that my code was supposed to do. $$anonymous$$y 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 $$anonymous$$ate
There's just one problem with your code. When I fire, it's a continuous stream ins$$anonymous$$d of the 1 every 0.5 seconds that the rate of fire is set up to. Any idea how to fix that?
sorry for the late reply, It does that because I was being a bit stupid, the if statement in the update should probably be...
if(Input.GetButtonDown("Fire1")){
InvokeRepeating("Shoot", 0, ROF);
}
if(Input.GetButtonUp("Fire1")){
CancelInvoke("Shoot");
}
Hopefully that works and glad it helps despite being unrelated to your question! sorry bout that
Thank you. It's better late then never, and I did end up getting it working. Thank you for all the help you gave.
Your answer
Follow this Question
Related Questions
Enemies Shoot Towards Face, Would Like to have them Shoot to Chest 1 Answer
Aiming with the mouse in a 2d shooter, and then shooting to the mouse 2 Answers
Bullets follow mouse after shooting in Sidescroller 1 Answer
Sidescroller Bullets Follow Mouse After Being Shot 2 Answers
Clones of enemies will not shoot 0 Answers