Hobbies And Interests

How to Shoot at the Mouse in Unity

"Unity 3D," more commonly known as simply "Unity," is a special game programming shell that is designed to streamline the video game creation process. By using Unity, video game programmers are able to bypass many of the pitfalls and glitches that can make game creation tedious and difficult. One of the main difficulties for newer users of "Unity" is figuring how to get your main character to shoot where the mouse is pointing. Remedy this issue by creating a MissileLauncher script that will allow you to dictate the behavior of your character's missile weapons.

Things You'll Need

  • FPS_Tutorial.zip
  • "Standard Assets Unity Package" file
Show More

Instructions

    • 1

      Download FPS_Tutorial.zip from the Unity 3D website. Import the "Standard Assets Unity Package" file from the "Standard Packages" folder in your Unity 3D installation folder.

    • 2

      Open the project for which you want to modify the mouse behavior. At this point, you should have already constructed a level environment, a model for your main character, a "Launcher," or weapon model, for that character, and a missile to be fired from the weapon. Keep in mind that the term "missile" is used in this case to describe any projectile that is fired from a weapon, not just an explosive rocket.

    • 3

      Select "Assets" from the main menu, followed by "Create," and then "Javascript." This will create a new asset in the Project Panel area entitled "NewBehaviourScript." Rename this asset to "MissileLauncher." This script asset will dictate the behavior of your character's weapon.

    • 4

      Create a fresh directory inside your Project View pane called "WeaponScripts." The scripts that define the behavior of each individual weapon will be placed here. Move the new MissileLauncher script into the WeaponScripts directory, along with the script that defines the appearance of your Missile object.

    • 5

      Write a script for "MissileLauncher" that will cause your weapon to generate a missile and make it move forward by giving it velocity along the Z-axis. After you have written the code, save the JavaScript file. A sample script would look something like:

      "var projectile : Rigidbody;

      var speed = 20;

      function Update()

      {

      if( Input.GetButtonDown( "Fire1" ) )

      {

      var instantiatedProjectile : Rigidbody = Instantiate(

      projectile, transform.position, transform.rotation );

      instantiatedProjectile.velocity =

      transform.TransformDirection( Vector3( 0, 0, speed ) );

      Physics.IgnoreCollision( instantiatedProjectile. collider,

      transform.root.collider );

      }

      }"

      The "GetButtonDown( "Fire1" )" line tells the script to execute the following actions when you click the left mouse button. The "instantiatedProjectile" line creates the missile object. The "TransformDirection( Vector3..." line tells the missile object to change its direction to the Z-axis, which is the axis where the user's mouse pointer is facing.

    • 6

      Attach the MissileLauncher script to your character's weapon by going to Unity's FPS Controller panel and attaching the script to the "Launcher."

    • 7

      Associate the Missile object that you have created with the "projectile" variable that is used in the sample script above. Click on the "Missile" script in the Project panel, and then click "Components" in the main menu bar. Select "Physics," and then "Rigidbody." This will make the projectile a solid object, and match it with the variable name that was used in the sample code.

    • 8

      Associate the Missile object with the Projectile variable in the script. Do this by clicking on "Launcher" in the Hierarchy panel, and then dragging the Missile script from the Project panel onto the Projectile variable in the MissileLauncher script section. The program will then associate the Missile object with its proper behavior.

    • 9

      Run the game to test your weapon's behavior. Assuming that your character's perspective is centered on the position of the mouse cursor, clicking the mouse button should fire your created missile along the vector where the mouse is pointing.


https://www.htfbw.com © Hobbies And Interests