x


projectile to cursor for a side scrolling shooter

I have a game project due, it is my final and our time frame is 9 days; which isnt that bad considering that I have a small scope for my game. The only hang up that I have run into is getting my character which does shoot, to be able to aim up and down on the y axis as the player moves along the x. A classmate told me about usinga Plane Collider in the background , set up a Vector 3 that calculates xyz but it has all gone over my head! I am an art guy by nature, drawing, modeling work, texturing is what I am most proficient so wrapping my head around code is an arguous task for me.... This is the code in which I am currently using to fire bullets:

var myCamera : Transform;
var prefab : Transform;
var player : GameObject;
// point of origin for particles
var flashpoint: Transform;
// actually particles
var muzzleFlash: GameObject;
var otherClip : AudioClip;

function Update () 
{
       if(Input.GetButtonDown("Fire1"))
       {
         Instantiate(muzzleFlash, flashpoint.transform.position, flashpoint.transform.rotation);
         var myBullet = Instantiate(prefab, myCamera.transform.position, myCamera.transform.rotation);
         //this tells the ingine to ignore detection of coliders between bujllet and player
         Physics.IgnoreCollision(player.collider, myBullet.collider);
         myBullet.rigidbody.AddForce(myBullet.transform.forward * 5000);
         if (!audio.isPlaying)
         {
          audio.clip = otherClip;
          audio.Play();
         }
       }
}

and this is some code I found that does what I want apparently, only I do not have the slightest as how to impliment it into my game or if it is c# or javascript syntax:

// Find the difference vector pointing from the weapon to the cursor
Vector3 diff = cursor.transform.position - weapon.transform.position;
// Always normalize the difference vector before using Atan2 function
diff.Normalize();

// calculate the Z rotation angle using atan2   
// Atan2 will give you an angle in radians, so you
// must use Rad2Deg constant to convert it to degrees
float rotZ = Mathf.Atan2(diff.y,diff.x) * Mathf.Rad2Deg;

// now assign the roation using Euler angle function
weapon.rotation = Quaternion.Euler(0f,0f,rotZ);
more ▼

asked Oct 28 '11 at 12:14 AM

v3ry gravatar image

v3ry
1 1 1 1

would it be possible to implement the code at the bottom into my already existing code at the top???

Oct 27 '11 at 10:18 PM v3ry

Yes- Learn C#, since it'll serve you better in the future, and doesn't have the weird fuzzy syntax that Javascript suffers from.

Oct 28 '11 at 12:20 AM syclamoth
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x129
x70
x56
x25
x10

asked: Oct 28 '11 at 12:14 AM

Seen: 626 times

Last Updated: Oct 28 '11 at 12:20 AM