Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Maggot · Oct 07, 2010 at 06:25 PM · parentchildbullet

Making an object follow another object (bullets following a player)

I want to have it so initially a bullet created by pressing/holding fire follows the player, by making it a child of the player parent, then this child/parent is broken when fire is released. How can i do this?

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
3
Best Answer

Answer by skovacs1 · Oct 07, 2010 at 07:50 PM

Parenting

Keep in mind that if the player rotates, the child object (your bullet) will rotate in space about the center point of the player.

You can achieve this by creating the object, parenting it and unparenting on release.

The script that fires your bullet

function Fire() {
    //do stuff....like create a bullet and get the player
    bullet.parent = player;
    //...
}

Somewhere else

function Update() {
    if(Input.GetButtonUp("Fire"))
        bullet.parent = null; //if on the bullet, use transform.parent
    if(parent) Something();
    else SomethingElse();
    //do stuff..
}

Other kinds of following

  1. Always move towards the player, whatever their position. Easy
  2. Follow the path that the player went along. Hard

If you're using physics to drive your bullet, that's potentially more complicated and I'd want to see some of your code to offer the best solution to your use case.

Moving towards the player

This is like a magic bullet that turns towards its target. If you want it to avoid walls and go around corners, you will have to add some ray casting and a few extras.

The script that fires your bullet

function Fire() {
    //do stuff....like create a bullet and get the player
    var script : BulletScript = bullet.GetComponent(BulletScript);
    if(script) script.target = player;
    //...
}

BulletScript.js

var target : Transform; var speed : float = 50.0;

function Update() { if(Input.GetButtonUp("Fire")) target = null; if(target) transform.LookAt(target.position, transform.up); transform.position += speed Time.deltaTime transform.forward; }

Following the player's path

This is like a ghost which will follow along the path the player followed. This requires you store the path the player has followed.

  1. This would be stored in array of Vector3 or of a custom data structure of several points representing bezier curves.
  2. You would have to add points in some smart way and this is dependent upon your movement system.
  3. Make the bullet move along the path by an amount related to its velocity.

Because this is highly dependent upon your movement system both for the player and the bullet, I would need to see your implementation in order to provide a solution.

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 Maggot · Oct 07, 2010 at 09:39 PM 0
Share

Thanks - parenting is what I wanted for the bullet, so it relatively sticks to the player until fire is released

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

No one has followed this question yet.

Related Questions

Make a simple tree 1 Answer

How to flip my Bullet with my Sprite 1 Answer

Calling a function of another object's child? 1 Answer

Trouble accessing child 3 Answers

parenting problem 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