Prefab Instance not moving

Hello. I’m having difficulty getting my code to do what I want. I’m supposed to hit space and a sword is supposed to shoot out. Instead, it only lands at my character’s feet:

// Sword Script - Gauntlet
// name

using UnityEngine;

// This is the Sword script.

public class SwordScript : MonoBehaviour
{

    // This is my timer.

    [SerializeField] private float memberDuration = 1.0f;
    private float memberTimer = 0.0f;

    // Start a Sword once when it's created.
    [SerializeField] private GameObject memberSwordPrefab = null;

    // Direction, but it's not working.
    [SerializeField] private float memberSpeed = 5.0f;
    [SerializeField] private Vector3 memberDirection = Vector3.one;
   
    // Rigid Body.
    private Rigidbody2D memberRigidBody = null;

    // Box Collider?
    [SerializeField] private GameObject memberBoxCollider2D;



    protected void Start()
    {
        memberTimer = memberDuration;
       
        // Get the rigid body.
        memberRigidBody = GetComponent<Rigidbody2D>();

        // Calculate the velocity.
        Vector3 localWhichVelocity;
        localWhichVelocity = memberSpeed * memberDirection;
       
        // You have to set the rigid body velocity to actually change an enemy's velocity.
        memberRigidBody.velocity = localWhichVelocity;


    }

    // Update a Sword every frame of the game.

    protected void Update()
    {
        memberTimer -= Time.deltaTime;

        if (memberTimer <= 0.0f)
        {
            this.gameObject.SetActive(false);
        }

        // Which direction does the player want to go?
        // Assume the player doesn't want to move, so no direction.

        Vector3 localWhichDirection;
        localWhichDirection = Vector3.zero;

        // Calculate the velocity.

        Vector3 localWhichVelocity;
        localWhichVelocity = memberSpeed * localWhichDirection;

        // You have to set the rigid body velocity
        // to actually change Valkyrie's velocity.

        memberRigidBody.velocity = localWhichVelocity;

    }
      
    void OnTriggerEnter2D(Collider2D localCollider)
    {
            Debug.Log("Trigger!");

            GameObject localOtherObject = localCollider.gameObject;

            // Check if the collided object is an enemy (Ghoul)
            if (localOtherObject.name.StartsWith("Ghoul"))
            {
                // Deactivate the collided object
                localOtherObject.SetActive(false);

            }
        // When spacebar is hit
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // instantiate the fireball object
            Instantiate(memberSwordPrefab,
                new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, 0),
                new Quaternion(0, 0, 0, 0));



        }
     


    }
}

In your Update function you set the velocity to Vector3.zero every frame.

Was I not supposed to? That’s the code I was given to work with. I’m a 8 week old beginner, so I am still learning.

That’s unfortunate. There’s so many details wrong here that it’s probably not a great starting point.

As dstears points out, you set the velocity to zero in Update. (Zero direction vector line 63, multiplied by scalar on line 68, will still be zero (of course) when in velocity, then assigned to velocity on line 73)… so not even sure what is the thinking behind that huge chain of 10 lines of code…

You’re also doing physics stuff in Update(), also not a great idea. Doable, but only when you understand the tradeoffs.

Then there is a Input.GetKeyDown() check within the OnTriggerEnter2D() call. Input.GetKeyDown() is only intended for use in Update, as specified in its documentation.

And then your comment says it’s making a fireball out of a sword prefab (is that just an out of date comment???) so I really don’t know what all is happening there.

You may wish to set this all gently aside and go revisit another tutorial for making gauntlet. Gauntlet is actually a really complicated game to get right, so you may wish to consider simpler stuff like Angry Birds, which can get you some familiarity with firing off projectiles, having them collide with stuff, etc.

In any case, one step at a time is the only way you will truly succeed at gamedev, like this:

Imphenzia: How Did I Learn To Make Games:

So, an update. I have movement, but it’s diagonally up/right. I can’t get it to move forward in front of me or any direction I’m facing:

// Sword Script - Gauntlet
// Quientin Fuller

using UnityEngine;

// This is the Sword script.

public class SwordScript : MonoBehaviour
{

    // This is my timer. 
    [SerializeField] private float memberDuration = 1.0f;
    private float memberTimer = 0.0f;

    // Start a Sword once when it's created.
    [SerializeField] private GameObject memberSwordPrefab = null;

    // Direction, but it's not working.
    [SerializeField] private float memberSpeed = 5.0f;
    [SerializeField] private Vector3 memberDirection = Vector3.one;
   
    // Rigid Body.
    private Rigidbody2D memberRigidBody = null;

    // Box Collider?
    [SerializeField] private GameObject memberBoxCollider2D;



    protected void Start()
    {
        // Duration
        memberTimer = memberDuration;
       
        // Get the rigid body.
        memberRigidBody = GetComponent<Rigidbody2D>();

        // Calculate the velocity.
        Vector3 localWhichVelocity;
        localWhichVelocity = memberSpeed * memberDirection;
       
       


    }

    // Update a Sword every frame of the game. 

    protected void Update()
    {
        memberTimer -= Time.deltaTime;

        if (memberTimer <= 0.0f)
        {
            this.gameObject.SetActive(false);
        }

        // Which direction does the player want to go?
        // Assume the player doesn't want to move, so no direction.
        Vector3 localWhichDirection = Vector3.one;

        // Calculate the velocity.
        Vector3 localWhichVelocity= memberSpeed * localWhichDirection;
       
        // You have to set the rigid body velocity to actually change Valkyrie's velocity.
        memberRigidBody.velocity = localWhichVelocity;

         // When spacebar is hit
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Vector3 localPosition;
            localPosition = this.transform.position;

            Instantiate(memberSwordPrefab, localPosition, Quaternion.identity);


        }
    }
       
    void OnTriggerEnter2D(Collider2D localCollider)
    {
            GameObject localOtherObject = localCollider.gameObject;

            // Check if the collided object is an enemy (Ghoul)
            if (localOtherObject.name.StartsWith("Ghoul"))
            {
                // Deactivate the collided object
                localOtherObject.SetActive(false);

            }
       
     


    }
}

You are now setting the direction to Vector3.one. This is equivalent to the vector (1, 1, 1). This will be diagonal up and to the right, but it will also travel in the Z direction, which you probably don’t want if this is a 2D game. If you want the direction to be based on the way the character is facing, then you’ll need to pass that information into this script somehow. You can’t rely on constant values like Vector3.one or Vector3.right.

I’m not sure how you have your scene set up or what your memberSwordPrefab looks like. Does the prefab have this script attached to it? It seems very odd that this object will disable itself after 1 second, but this is also the object that is looking for the Space key.

I see that you are using SetActive(false) when an object is not needed. Note that this keeps the object in the scene, it just won’t do anything. If you really don’t need the object anymore, then you should use the Destroy function to delete the object.

I agree with Kurt that you may want to check out some more tutorials or the Unity Learn series to get a better understanding on how stuff interacts in Unity.

Thank you both for the help. As I told Kurt, this is the code I was given to start. I changed the Quarternion to:

Instantiate(memberSwordPrefab, localPosition, Quaternion.identity);

as that was provided to us. I’m definitely going to check out tutorials, but the ones I found didn’t conclusively help.

That’s not how tutorials work.

Not sure where you got that impression.

Tutorials at best may serve to inform you how to do the very next single thing, one step at a time, as the video I linked above.

I’ll link it again for you because this will be how you learn the process, even if you try other ways.

Imphenzia: How Did I Learn To Make Games:

I watched it. I am taking things one step at a time. I’m trying to figure out the next step now.