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 Hogge · Jan 14, 2014 at 11:15 PM · 3dparentinglocalposition

parent and .localposition?

I'm currently working on a scrolling shooter which gives more of the visual impression of being a fully fledged 3D flight game (which I'm later going to turn into something more along the lines of an Afterburner-ish thing). An important part of this is to make both bullets and enemy vehicles move together with the gameplay area.

Now, I've read enough about this stuff to know that I need to use parenting and .localposition to solve the problem. I am however such a noob at programming (I began programming in Unity two days ago), that I don't quite know HOW to use those two things. The script on the bullet currently looks like this:

 using UnityEngine;
 using System.Collections;
 
 public class Projectile : MonoBehaviour {
 public float ProjectileSpeed;
 
     // Use this for initialization
     void Start () {
 
     }
 
     // Update is called once per frame
     void Update () {
         float amtToMove = ProjectileSpeed * Time.deltaTime;
         transform.Translate (Vector3.back * amtToMove);
     }
 }

Also, I've put the player object inside an object called GameplayArea, which should be the parent of all other game objects. Could someone help me make this work?

Comment
Add comment · Show 5
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 craigaw · Jan 17, 2014 at 05:42 AM 0
Share

I'm not sure exactly what the problem is here. What exactly isn't working?

avatar image Hogge · Jan 19, 2014 at 01:30 AM 0
Share

Expression denotes a 'type', where a 'variable', 'value' or 'method group' was expected. Any ideas?

 using UnityEngine;
 using System.Collections;
 
 public class Projectile : $$anonymous$$onoBehaviour {
 public float ProjectileSpeed;
 public Transform iTweenrider;
     // Use this for initialization
 
     void Start () {
         GameObject theProjectile = Instantiate(Projectile,PlayerCannon ,Quaternion.identity);
         theProjectile.transform.parent = iTweenrider;
         }
     // Update is called once per frame
     void Update () {
         float amtTo$$anonymous$$ove = ProjectileSpeed * Time.deltaTime;
         transform.Translate (Vector3.back * amtTo$$anonymous$$ove);
     }
 }
 
avatar image getyour411 Hogge · Jan 19, 2014 at 01:33 AM 0
Share

Also, please review my previous comment, I don't think you want Instantiate on this class unless I've really misunderstood how you are using it.

If you post an error, post a line# Source is probably the use of 'PlayerCannon' - what is that to this class? I don't see it defined anywhere. Finally, dont' post an update as an Answer, I converted this one for you.

avatar image Hogge · Jan 19, 2014 at 02:27 AM 0
Share

O$$anonymous$$, it seems like we've both misunderstood each other. I believe that the code snippets you've been tipping me about were supposed to be attached to the cannon prefab, which is why you're using instantiate, right? While I was showing you the code for the bullet itself, where it would have been more logical for it to parent itself to the iTweenrider as soon as it awakens, right?

I've moved the codesnippet to the cannon script, so now it looks as follows:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerCannon : $$anonymous$$onoBehaviour {
     public GameObject ProjectilePrefab;
     public class iTweenrider ;
     // Use this for initialization
     //void Start () {
     
     //}
     
     // Update is called once per frame
     void Update () {
         if (Input.Get$$anonymous$$eyDown ("space")) {
             Vector3 position = new Vector3(transform.position.x, transform.position.y +(transform.localScale.y / 2), transform.position.z);
             //Instantiate (ProjectilePrefab, position, Quaternion.identity);    
             
             gameObject Projectile = Instantiate(ProjectilePrefab, position ,Quaternion.identity);
             ProjectilePrefab.transform.parent = iTweenrider;
             
         }
     }
 }

This has reduced my error messages from four to one, so I hope that my thinking is right. The hopefully last error before this works is a complaint that there's an unexpected ';' at the end of row 6.

avatar image getyour411 Hogge · Jan 19, 2014 at 02:29 AM 0
Share

That's three times you've posted a comment as an answer in this one thread. I changed this AGAIN. Is it not clear what a comment/follow-up vs an Answer is?

And yes, you have it right everything is making way more sense now. The issue on line 6 is you have public class iTweenRider and it should be public Transform iTweenRider (and then drag/drop your iTweenRider GObj into the slot that appears in your Inspector); also change this

  gameObject Projectile = Instantiate(ProjectilePrefab, position ,Quaternion.identity);
 ProjectilePrefab.transform.parent = iTweenrider;

to this

 GameObject Projectile = Instant......
 Projectile.transform.parent = .....


2 Replies

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

Answer by Headworker · Jan 18, 2014 at 10:39 PM

You can use the transform of the bullet itself to propel it forward:

 void Update () 
 {
 transform.position = transform.position + transform.forward * Time.deltaTime * 5f;
 }

Depending on what you want to achieve, I would tell you to use ray-hitscanning or projectile with rigidbody and use of forces for your projectiles.

Comment
Add comment · Show 12 · 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 Hogge · Jan 18, 2014 at 09:23 PM 0
Share

I'm not sure why my replies aren't showing up, Craig. What I want is to create a Starfox-style rail-shooter experience. To make this work, the bullets I fire should move forward in the local coordinates of the gameplay area-prefab I've created, rather than in worldspace. The problem is perhaps not as much that something doesen't work, as I don't know how to use localspace coordinates.

avatar image Hogge · Jan 18, 2014 at 11:01 PM 0
Share

I've already figured out how to make the projectile forward. The issue is that I don't know how to parent the bullet to the GameArea prefab, to make it move in localspace.

avatar image getyour411 · Jan 18, 2014 at 11:09 PM 0
Share

@Hogge please don't post a follow-up as an answer; I fixed this one for you. Once you achieve a certain karma level, your posts won't need to be first approved by a moderator (i.e. the delay in your replies showing up)

avatar image getyour411 · Jan 18, 2014 at 11:10 PM 0
Share

Re: how to parent something -

 GameObject myThing = Instantiate(obj,pos,rot);
 myThing.transform.parent = someOtherGObj;

(such as your "GameArea")

avatar image Hogge · Jan 19, 2014 at 12:05 AM 0
Share

Oh, I'm so sorry. Umm... I'm really sorry, but as I stated before, I'm very new at program$$anonymous$$g in Unity and in general. Where do I put that snippet of code? I keep on getting error messages that term "instantiate" doesen't exist. Also, what's a suitable position and rotation?

Show more comments
avatar image
0

Answer by Hogge · Jan 19, 2014 at 10:23 PM

I've finally managed to make it work... partially. ![alt text][1] The problem here is that, while the bullets are parented to the the plane, they're not programmed to be launched in the direction that the plane is flying, but rather in worldspace. This image hopefully clarifies things. The white lines are trails after the bullets. All bullets should follow each other, since I haven't provided any input beyond shooting.

This is the script I'm using now:

 using UnityEngine;
 using System.Collections;
 
 public class Projectile : MonoBehaviour {
     public float ProjectileSpeed;
     public Transform iTweenrider;
     // Use this for initialization
     void Start () {
         
     }
     void Awake () {
         transform.parent = iTweenrider;
         //GameObject Projectile = Instantiate(Projectile,PlayerCannon ,Quaternion.identity);
         //Projectile.transform.parent = iTweenrider;
     }
     // Update is called once per frame
     void Update () {
         float amtToMove = ProjectileSpeed * Time.deltaTime;
         transform.Translate (Vector3.back * amtToMove);
     }
 }

The (hopefully) last thing I'll need is to figure out how to make the bullet fire in the direction the Player object is facing. [1]: /storage/temp/20861-scrollingshooter3cut.jpg


scrollingshooter3cut.jpg (103.4 kB)
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

19 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

Related Questions

Logic Question with position and parenting 2 Answers

Instantiate Object At Local Position 2 Answers

How to get localPosition to work on parented object C# 1 Answer

Object starts floating after being fixed to bones 0 Answers

How to get max and min points of a game object on the x and z axis? 3 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