- Home /
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?
I'm not sure exactly what the problem is here. What exactly isn't working?
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);
}
}
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.
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.
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 = .....
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.
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.
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.
@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)
Re: how to parent something -
GameObject myThing = Instantiate(obj,pos,rot);
myThing.transform.parent = someOtherGObj;
(such as your "GameArea")
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?
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
Your answer
Follow this Question
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