Hello, I am developing a 2D shooter and I have some questions regarding the scripting. I am able to understand the basic logic and process of what needs to be done
1: How would I add a score to the game and then display it using the GUI skin (not just plain text)? How would I then access (call?) the function that increases the score from any script (with a variable amount of points) so I can have many things that increase the score?
2: How would I display a timer on the GUI skin?
3: When the mouse is clicked, I want to make the bullet object move outward from the player in the direction of the mouse cursor (and If the mouse it held down, it would be rapid fire), how would this be done? The game is along the ZX plane if that helps.
4: How would I make a crosshair decal move 1 to 1 with the mouse cursor?
5: How can I spawn objects using the score/timer values (ex: at 1 minute spawn Z about of object A)?
Thank you. I know I have alot of complex questions.
The score can simply be displayed using a GUI.Label in function OnGUI();
You can have a local variable which holds the actual score int, which you can display in the label.
I haven’t done anything like this, but I would imagine making a GUI Texture, and changing the position of the element according to the mouse position(see the script reference for the mouse pos)
In its un-modified version, it would just should shoot another object up (along the Z axis, since its a 2D game).
But when I modified it to this (I swapped for Event.mousePosition) it makes the object it shooting move very quickly to the side (and sometimes straight at the camera). Whats wrong?
var projectile : Rigidbody;
var speed = 2;
function Update()
{
if( Input.GetButtonDown( "Fire1" ) )
{
var instantiatedProjectile : Rigidbody = Instantiate(
projectile, transform.position, transform.rotation );
instantiatedProjectile.velocity =
transform.TransformDirection
(Event.mousePosition);
}
}
To get a vector from the current player position to the mouse position, you need to use vector subtraction to get a vector pointing in the right direction:-
var fireDirection = Input.mousePosition - transform.position;
To launch a projectile in that direction, you shouldn’t use this vector directly since it will affect the launch speed. Instead, normalise the vector and multiply it by the speed you want to fire with:-