Answered Make a projectile teleport to the player instead of cloning. (Unity 2d)

BLSturton

New member
Joined
Sep 19, 2020
Messages
1
Programming Experience
Beginner
I'm trying to make a 2d platformed. In it the player can shoot a projectile. I've been following a tutorial where they make it so the bullet makes a clone of itself. I want it so the bullet can teleport to the player and move (so that only 1 can be on screen at a time.) But he has it so the bullet clones itself instead. I THINK the problem is in line 15, but I'm not sure what to do. Any help would be great!
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAttack : MonoBehaviour
{
     public Transform firePosition;
     public GameObject projectile;
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetButton("Fire1"))
         {
             Instantiate(projectile, firePosition.position, firePosition.rotation);
         }       
     }
}

here's the video for context

Add comment
 
Last edited by a moderator:
Welcome to the forums. Please post your code in code tags in the future.
 
I've not viewed the video link, but I would assume that is the same place where it is also cloned as well. So don't call Instantiate(). Just change the position of the projectile back to the starting position and rotation that you want instead of letting it clone another copy.
 
I haven't looked at the video either, but I recall this from the docs and it looks awfully familiar to the docs had in mind Unity - Scripting API: Object.Instantiate

I would recommend you clone the object and set a new vector for the rotation or angle and destroy the original if it impacts something or goes outside of a viewable distance.

I'd also recommend you learn by reading the docs provided by Unity and not follow a tutorial where the code in the video often won't reflect current modern-day API due to updates made in the Unity Engine.
 
The OP is looking to implement something like Thor's Hammer or Captain America's shield where there is ever only one such object in the game, but instead of the seeing the item fly back to the player, it just instantly returns to the player. If there is just one item in the world, why even create a new one? Just change it's position.
 
Sorry I don't have time to sit around watching tutorials. But if you look at the docs i linked, they are very closely resembling the code being used.

Have you read the docs I linked Skydiver?

If they want to keep the current object, then simply changing the position might work, or it might look weird. Trial and error... try and find out
 
Yes, I read the docs. And it said that it clones the object that is passed in. The OP was asking how not to clone the object.
 
I wasn't directing him to clone the object. Perhaps i will be more clear :
After cloning an object you can also use GetComponent to set properties on a specific component attached to the cloned object.
I don't see how calling GetComponent on the current object can't be used to set new parameters. Assuming its the projectile they want to return? Then GetComponent will do fine as the docs suggests. OP will need to reverse the action first initiated to instantiate that object from the other object it came from. You know I'm busy with the new CMS project, so I'm not in a position to write out a whole new script at present.

I should also point out this is a third party product, so any support on it will be limited as it's not directly a C# issue, it's a Unity scripting issue. A quick search for GetComponent explains how to use it, and with a little digging, you can come up with something similar to this script : https://forum.unity.com/threads/how-change-object-direction-after-collision.543150/

I will also mark this as answered, as it's currently answered with the best possible recommendations and advice, unless someone else wants to volunteer to write a whole script for the OP...
 
Back
Top Bottom