I am trying to make a game in C# with Unity and I’m trying to make the Space Bar shoot a Projectile.
I’m trying to Instantiate my Projectile Prefab, but nothing seems to appear when I go to test it.
Here is my Player Script:
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
public float PlayerSpeed;
public GameObject ProjectilePrefab;
public int theScore = 0;
// Update is called once per frame
void Update () {
//PlayerSpeed
float amtToMove = Input.GetAxis("Horizontal") * PlayerSpeed * Time.deltaTime;
transform.Translate(Vector3.right * amtToMove);
//These two lines are all there is to the actual movement..
float moveInput = Input.GetAxis("Horizontal") * Time.deltaTime * 3;
transform.position += new Vector3(moveInput, 0, 0);
//Restrict movement between two values
if (transform.position.x <= -19.0f || transform.position.x >= 19.0f)
{
float xPos = Mathf.Clamp(transform.position.x, -19.0f, 19.0f); //Clamp between min -19.0 and max 19.0
transform.position = new Vector3(xPos, transform.position.y, transform.position.z);
if (Input.GetKeyDown("space"))
{
//Fire Projectile
Instantiate(ProjectilePrefab, transform.position, transform.rotation);
}
}
}
Thank You.
Update
I am only able to shoot my projectiles when only at the far left or far right side of the screen. How am I able to fire anywhere on the screen while still be able to stop the player at the edge of the screen?
For ease of use, you may want to use the KeyCode enum within the GetKeyDown method.
instead of:
if (Input.GetKeyDown("space"))
{
//Fire Projectile
Instantiate(ProjectilePrefab);
}
use:
if (Input.GetKeyDown(KeyCode.Space))
{
//Fire Projectile
Instantiate(ProjectilePrefab);
}
Second, when you hit the spacebar key, do you see that a new ProjectilePrefab is being added to the inspector in unity, you don’t have any force being added, so it isn’t going to move unless it has a movement script attached to the prefab, but do you see it being created, not visually, but in the inspector?
You may also want to put a Debug.Log statement in side your code, when you “restrict” the movement with transform.position.x <= -19.0f || transform.position.x >= 19.0f, is that condition being met?
Are you getting an error in the Console window when you try to execute this code? If so, you likely do not have ProjectilePrefab setup. If you have it setup, the next problem is that you don’t do anything with the projectile. That is, you create it, but you don’t position it, rotate it, or add force to make it shoot. It will appear in the same position as when it created in the editor (which may not even be visible). And, if it has a Rigidbody with uses gravity, it will promptly drop down due to gravity. If you look in the Hierarchy window you should see the projectile you just instantiated.
Now the next question is where to fire it from. Typically a spawn point just in front of the gun is selected as the position to put the projectile when it is created. Usually I see the firing script attached to that spawn point rather than to the movement code for the gun itself. There’s no aiming code here yet, but to fire it you would do something like:
GameObject go = Instantiate(ProjectilePrefab, transform.position, transform.rotation) as GameObject;
go.rigidbody.AddForce(transform.forward*1000);
With a projectile with the default 1 unit of mass, it takes 500 - 2000 units of force to get it really going.
I think I see a few problems in your script. Instantiate needs a position and rotation to be specified. What I alway do is make a empty game object for a spawn point and make it a child of what is shooting. Then I Instantiate the projectile at the position and rotation of the spawn point:
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
var spawnPoint : Transform;
var projectile : Transform
void Update(){
if(Input.GetButtonDown("Jump")){
Instantiate(projectile, spawnPoint.position, spawnPoint.rotation) as Transform;
}
}
}