How to store a position that is moving to create a prefab to spawn at the exact position
Hey guys,
Sorry if the post is not standard format, I will try to make it as simple as possible. With out everyone else saying the same thing, but I am also new to C# and coding in general (Self teaching) so there is a lot of beginner mistakes and format with the code.
Now what I am trying to achieve is that in my game the player fires an arrow with one button and it is just the basic arrow. Now with the second attack it is meant to fire an arrow and then when I press another button (Prefer to make it same button, but struggling with that too) as the projectile moves along the x axis, I want it to grab that current position and Instantiate another prefab in its place. As this may be simple to others, I am actually struggling to find much material, however I have found certain scripts that help a little bit. Currently what is happening, the prefab spawns at 0,0,0 but that is expected based on the code I have at the moment, but the previous way it was creating it at the point the arrow is fired. Here is the firing code I am using:
using UnityEngine;
using System.Collections;
public class Firing : MonoBehaviour
{
public Transform playerBullet;
public Transform playerBullet2;
public float timeBetweenShots = 0.5f; // Allow 1.5 shots per second
public GameObject treantPrefab;
private float timestamp;
private Player_1 player1;
private float fireRate = 10.5f;
private float nextFire = 0.0f;
private bool squarePressed = false;
private float arrowX;
private float arrowY;
private float arrowZ;
// float fire = Input.GetAxis ("Fire5");
// Use this for initialization
void Start ()
{
player1 = GameObject.Find("Player_1").GetComponent<Player_1>();
}
// Update is called once per frame
void Update ()
{
if (Time.time >= timestamp && Input.GetButtonDown ("Fire5"))
{
Transform bullet = (Transform)Instantiate (playerBullet, transform.position, Quaternion.identity);
playerBullet.GetComponent<BulletController> ().SetDirection (player1.faceDir);
timestamp = Time.time + timeBetweenShots;
}
if (Input.GetButtonDown ("PS4_Square") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Transform bullet2 = (Transform)Instantiate (playerBullet2, transform.position, Quaternion.identity);
playerBullet2.GetComponent<BulletController> ().SetDirection (player1.faceDir);
transform.position = playerBullet2.position + new Vector3 (arrowX, arrowY, arrowZ);
squarePressed = true;
}
if (squarePressed = true && Input.GetButtonDown ("PS4_Triangle"))
{
DestroyImmediate (playerBullet2.gameObject, true);
Instantiate (treantPrefab, new Vector3 (arrowX, arrowY, arrowZ), Quaternion.identity);
// Instantiate (treantPrefab, playerBullet2.localPosition, Quaternion.identity);
squarePressed = false;
}
}
void LateUpdate()
{
}
void SummonTreant()
{
}
}
Answer by fredz0004 · Nov 23, 2015 at 08:06 AM
Hi ThyPanda for the prefab instantiate. I would create a script or maybe even in the same one you have right now to check the position of the current position of the prefab, at a certain interval maybe every second or maybe less. So you are checking lastPosition vs currentPosition. If the currentPosition is different than lastPosition then update lastPosition. Is important to get this x,y,z lastPosition before the object is destroyed otherwise it would be impossible to know where to instantiate your new prefab.
Once you want to instantiate the prefab, use those lastPosition x,y,z coordinates for the new instance prefab.
I hope this makes sense.