How can i change the position of my shot ?
Hi all,
One more yeah, i'm a newbee.
I'm following a tutorial to make my first shotgame 2D but i'm trying to understand the details of the scripts.
Actually i've a ship which can fire ammo. The part of the creation of the ammo is this :
// Assign position
shotTransform.position = transform.position;
shotTransform.position += new Vector3(1.1f, 0, 0);
To be clear, the shotTransform is my ammo and the transform is just a component of my Player 1. Actually there is no problem to fire but the ammo starts since the middle of my ship. So i tried to add a horizontal vector to make it starts from the right side of my ship !
So actually i'm stucked here... Someone can tell me why my ammo starts again in the middle ? (1.1 is the distance betwen right side and the middle of the ship)
Thank you for your help and have a good week ! :p
Answer by incorrect · Jan 20, 2016 at 12:35 AM
Please, format the code using Markdown Syntax.
Your shots are fired from the point transform.position, so it's the point where transform of an object you have attached this script is placed at the moment. If it's attached to the ship, this will result in getting ship's position.
To make it work a bit more natural I'll suggest doing so:
Add an empty GameObject to your ship as a child and place it somewhere you want your shot to be fired from.
After that add this lines to the script:
[SerializeField]
private Transform firingPoint;
What they actually are doing is adding a reference to some Transform which is not accessible with other scripts due to 'private' modifier, but can be assigned in Inspector by drag'n'dropping. So place your newly created firing point there.
And after that to fire you have to place the shot in that point:
shotTransform.position = firingPoint.position;
Hi Incorrect,
Thank you for your tips with the child. I've had little difficult to find the transform.Find("firingPoint").position but it works ! :D
What I suggest is not using transform.Find("firingPoint"), but to assign it using Inspector.
Your answer
Follow this Question
Related Questions
Collider and Rigidbody Problem 1 Answer
keep a 2d sprite in an area via color 0 Answers
Position changing though set position code running 1 Answer
iscometric grid panning 0 Answers