- Home /
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);
would it be possible to implement the code at the bottom into my already existing code at the top???
Yes- Learn C#, since it'll serve you better in the future, and doesn't have the weird fuzzy syntax that Javascript suffers from.