Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ThyPanda · Nov 22, 2015 at 02:20 PM · instantiateprefabposition

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()
     {
 
     }
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

39 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiated prefab trying to keep weird Y position 0 Answers

Instantiate not spawning at correct position 1 Answer

[Solved] Instantiated objects spawn at diferrent x 1 Answer

Cannot convert UnitEngine.Vector3 to UnityEngine.Transform when using .postition 1 Answer

Instantiate prefab if no prefab exists at location 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges