Bullet is not moving forward

Here is my player controller script;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

    public float speed = 15.0f;
    public float padding = 1f;
    public GameObject projectile;
    public float projectileSpeed;
    public float firingRate = 0.2f;

    private Rigidbody2D PlayerLaser;

    float xmin;
    float xmax;

    void Start()
    {
        float distance = transform.position.z - Camera.main.transform.position.z;
        Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0,0,distance));
        Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distance));
        xmin = leftmost.x + padding;
        xmax = rightmost.x - padding;
        
    }

    void Fire()
    {
        GameObject beam = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
        PlayerLaser.velocity = new Vector3(0, projectileSpeed, 0);
    }
    void Update()
        {
            if (Input.GetKeyDown(KeyCode.Space))
        {
            CancelInvoke("Fire");
        }
        {
            InvokeRepeating("Fire", 0.000001f, firingRate);
        }
            if (Input.GetKeyUp(KeyCode.Space))
            if (Input.GetKey(KeyCode.LeftArrow))
            {
                transform.position += Vector3.left * speed * Time.deltaTime;
                print("L arrow down");
            }
            else if (Input.GetKey(KeyCode.RightArrow))
            {
            transform.position += Vector3.right * speed * Time.deltaTime;
            print("R arrow down");
        }

        float newX = Mathf.Clamp(transform.position.x, xmin, xmax);

        transform.position = new Vector3(newX, transform.position.y, transform.position.z);
    }

}

Also showing an error

I think you Instantiate a beam and then try to give velocity to a PlayerLaser, instead of giving a velocity to the instantiated gameobject.

I have this (Unity 5):

GameObject laser = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
laser.GetComponent().velocity = new Vector3(0, projectileSpeed, 0);

Also, I assume you are setting the projectileSpeed in the Unity editor

Not working, now what shoul I do?

Have you dragged the bullet prefab to be attached to the script on the player? It seems like you’re trying to instantiate something that is set to null.

yes

also getting an error

Can you please show the full error, only the first line of it doesn’t help debug the issue.

Also can you also include a picture of Unity when you have the player ship selected so we can see the editor properties for the script.

1 Like

Look at the bullets

When you replied to @Duckfest_JF above and stated “not working”, what did you actually try?

From what I can tell from your code, you are trying to access the velocity of the Rigidbody called PlayerLaser, but at no point in the code is PlayerLaser instantiated. In @Duckfest_JF’s example, he uses a different variable name, if you simply copy/pasted that, it probably wouldn’t have worked as the example was quoted instead of applying code formatting, as such the < and > characters have not been rendered in his post.

The screenshot emphasizes this I believe. You instantiate a new projectile and it appears in the scene, but because you have no references to it’s Rigidbody component, you are just placing them one after the other, whilst moving your ship side to side, hence the block of projectiles.

The original error message is still displayed, it’s still line 31, which is this line;

PlayerLaser.velocity = new Vector3(0, projectileSpeed, 0);

Try this (long way);

void Fire()
{
    GameObject beam = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
    PlayerLaser = beam.GetComponent<Rigidbody2D>();
    PlayerLaser.velocity = new Vector3(0, projectileSpeed, 0);
}

or (short way);

GameObject laser = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
laser.GetComponent<Rigidbody2D>().velocity = new Vector3(0, projectileSpeed, 0);

…and then you can comment out/delete the declaration for PlayerLaser if you are not using this anywhere else.

1 Like

Thanks, Rob
As duckfest said I was using that line of code without reference of Rigidbody like this

void Fire()
    {
        GameObject PlayerLaser= Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
        PlayerLaser.GetComponent().velocity = new Vector3(0, projectileSpeed, 0);
    }

as duckfest mentioned in earlier reply

that is why it was not working.

Now it is working and by the way, I solved the problem of the automatic firing of bullets, I was using cancel invoke in GetKeyDown.

1 Like

Thank you.

1 Like

You’re welcome.

Just as a side note, to format the code when you copy/paste it, you need to precede the code with ``` and after the code place ```, I’ve updated your previous reply Vivek.


See also;

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms