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 /
avatar image
0
Question by AsAnAsterisk · Jun 28, 2017 at 06:00 AM · scripting problemmovementpositionprojectilechild object

How to Have Projectile Reset to Initial Position Relative to Parent

I have an enemy that fires projectiles during it's animation, and the projectiles reset their position once the animation is complete. I've pretty much worked everything out, except the position the projectile resets to is relative to the entire scene, when i'd prefer it be relative to the object the projectile is a child of.

(self is the projectile game object, and body is the enemy game object)

 public class StiltRanged : MonoBehaviour {
 public Rigidbody bullet;
 public Transform player;
 public GameObject self;
 public GameObject body;
 public Vector3 moveDirection;
 Vector3 initialPosition;
 Quaternion initialRotation;
 
     
     // Use this for initialization
     void Start () {
         initialPosition = new Vector3(0, 0, 0);
         initialRotation = new Quaternion(0, 0, 0, 0);
         
         
     }
     
     // Update is called once per frame
     void Update () {
         
         if (self.activeInHierarchy == true) {
         bulletFire();
         }
         if (self.activeInHierarchy == false) {
         bulletReturn();
         }
     }
     
     
     void bulletFire() {
         moveDirection = player.transform.position - body.transform.position;
         bullet.AddForce(moveDirection.normalized * 20f);
     }
     void bulletReturn() {
         self.transform.position = initialPosition;
         self.transform.rotation = initialRotation;
         }
         
 }
Comment
Add comment · Show 2
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 neosca · Jun 28, 2017 at 07:22 AM 0
Share

Are your trying to reset the position of "bullet" or "self"? In your code there is a Rigidbody bullet, and a Gameobject self. From your query it seems like you are trying to reset the projectile position which should be the "bullet", however, in the void bulletReturn() you are parsing values to "self". One more question, does your character(enemy) have movement? If that's the case you would need to get updated position of your enemy ins$$anonymous$$d of a default..

avatar image AsAnAsterisk neosca · Jun 28, 2017 at 07:09 PM 0
Share

ah, my bad, "self" is the game object of the projectile, "bullet" is the rigidbody component of that same game object, and yes the enemy has movement, also the script isn't attached to the projectile itself because it needs to know if the projectile is active or not, if that helps.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Trevdevs · Jun 28, 2017 at 07:53 AM

Remember that position and rotation are the global ones in the scene if you want to access the specific objects current position and rotation its best to use localrotation

agreeing with the question ask by neosca not sure about the self part or the bullet but it should be

transform.localposition and or transform.localrotation

this may help you to

https://docs.unity3d.com/ScriptReference/Transform-localRotation.html

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

131 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

Related Questions

AddForce/ForceMode.Impulse 1 Answer

how to stop the camera to follow the player on his jump movement ? 2 Answers

Smooth Camera/Object Movement 1 Answer

AI Movement Direction 0 Answers

Instantiate creating object far off the screen 0 Answers


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