Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by TheManFromMars · Apr 25 at 11:50 PM · scripting problemtransformprefabsinstantiationparent transform

Firing projectile from Transform. What am I doing wrong?

I've made a simple RTS style setup just using basic objects from Unity. I have a Unit, a weapon and projectiles prefabs. The weapon is attached to the unit via a transform. I can move the unit around and the weapon follows as it's supposed to. However when I go to fire the projectile, the projectile continues to spawn from the center of where the Unit starts. No matter what I do the bullets just spawn where the unit used to be and go on their way.

I've attached a transform to the weapon at the exit point I want the projectiles to come from. I've made sure to drag and drop it in the inspector. I've used Debug.Log to discover that the weapon thinks it's at (0, 0, 0) but I don't understand why. I've attached the code for the Unit and the Weapon below.

I want the ammo projectiles to spawn from the end of the weapon even if the Unit has moved and it should fire in the direction the Unit is facing.

Any help would be greatly appreciated. I've found many answers here but this is the firs time I've had to post to get help. Hopefully I've posted this in the right place and formatted/given the right information.

Unit Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class Unit : MonoBehaviour
 {
 
     // Properties
     public string unitName;
     public int hp;
     public Weapon weapon;
     private Weapon gun;
     public Transform weaponSlot;
     private MeshRenderer rend;
     private AudioSource _audio;
     public AudioClip clip_selected;
     public AudioClip clip_affirm;
     public bool selected = false;
     private NavMeshAgent navAgent;
 
     // Start is called before the first frame update
     void Start()
     {
         Vector3 ws = weaponSlot.position;
         rend = GetComponentInChildren<MeshRenderer>();
         _audio = GetComponent<AudioSource>();
         gun = Instantiate(weapon, ws, Quaternion.Euler(90, 0, 0));
         gun.transform.SetParent(weaponSlot);
         navAgent = GetComponent<NavMeshAgent>();
         rend.material.color = Color.black;
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetMouseButtonUp(0))
         {
             RaycastHit hit = new RaycastHit();
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit) && hit.transform.tag == "Marine")
             {
                 _audio.PlayOneShot(clip_selected);
                 selected = true;
                 
             }
         }
 
         if (Input.GetMouseButtonDown(0) && selected == true)
         {
             RaycastHit hit = new RaycastHit();
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit) && selected == true)
             {
                 _audio.PlayOneShot(clip_affirm);
                 navAgent.destination = hit.point;
 
                 
             }
         }
         if (Input.GetMouseButtonDown(1))
         {
             selected = false;
         }
 
         if(Input.GetKeyDown(KeyCode.Space))
         {
             gun.Fire();
         }
 
     }
 
     private void OnMouseEnter()
     {
         
         rend.material.color = Color.white;
     }
     private void OnMouseExit()
     {
         rend.material.color = Color.black;
     }
 
 }
 

Weapon Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Weapon : MonoBehaviour
 {
     // Properties
     public string weaponName;
     public int rateOfFire;
     public int damage;
     public AudioClip fire;
     public Ammo ammo;
     public Transform gunExit;
     Vector3 v_gunExit;
     AudioSource _audio;
     
 
 
     // Start is called before the first frame update
     void Start()
     {
         
         _audio = GetComponent<AudioSource>();    
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     public void Fire()
     {
         Debug.Log(this.transform.localPosition);
         Instantiate(ammo, gunExit.localPosition, Quaternion.identity);
         
 
     }
 }
 
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 graydogstudios · Apr 26 at 02:12 AM

Line 35: Instantiate(ammo, gunExit.localPosition, Quaternion.identity);

And earlier you have:

public Transform gunExit

This is going to be (0,0,0) assuming weapon is attached to player. Thus it will come out of the player position.

Other than that, Id have to see the xyz in inspector. This is because:

gun.transform.SetParent(weaponSlot);

So gun would be a child of weaponSlot, which is a child of CharacteR?

Comment
Add comment · Show 1 · 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
avatar image TheManFromMars · Apr 26 at 04:27 AM 0
Share

Thanks for the feedback. It doesn't come out of the player but from (0,0,0) on the 'map'. It just spawns in mid air no matter where the player is. The gun is a child of weaponSlot which is a child of character yes. I got it to fire from the player location but not the correct direction by passing the gun.transform.position to the Fire() function but it still just shoots straight up or down AND for some reason the character 'jitters' when I fire. Its supposed to come out of the end of the gun but doesn't.

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

315 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 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 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

how to access the Transform of a moving child object? 1 Answer

Bizarre Respawn Bug 0 Answers

Prefab Scripts Not Working After First Spawn? 2 Answers

Placing and rotating dominoes? 1 Answer

[HELP] Setting the parent of a transform which resides in a prefab is disabled 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