mhh i thought it was the one variable which would go outside the method ahahha but why the waypoint never change their position even if i do "sum " of other vectors on them?
startingPosition
as an instance variable is correct. However, if you could get that position easily each frame, there would be no need to cache it.
Here is the solution for startingPosition
as I imagined it:
Vector3 startingPosition;
void Start()
{
startingPosition = transform.position;
waypointsBoss = FindObjectOfType<Boss>().GetWaypoints();
}
Do not override the variable in Update.
BossPathing is assigned to the projectile, isn’t it? If so, this should fix it.
I’ve just realised that you actually don’t need 3 variables. If you assign the correct value to startingPosition
, your solution should work.
Yes BossPathing is attached to the projectile
ok wait you’ll think i’m an idiot but it’s 20 minutes that i’m working on the wrong script
it should be MissilePathing not BossPathing, it’s a bit late in Italy so i’m a lil sleepy if i can justify me somehow ahaahahah that was the script for the movement of the boss, i screwed up a bit now and i messed up that script and it’s not working too ahaha
ok i figured it out the boss path was not working anymore cause i changed it with the previous script we did ahahah now i’ll implement all we did on the RIGHT script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MissilePathing : MonoBehaviour
{
List<Transform> waypointsMissile;
int waypointIndex = 0;
float moveSpeed;
Vector3 startingPosition;
void Start()
{
startingPosition = transform.position;
waypointsMissile = FindObjectOfType<Player>().GetWaypoints();
}
void Update()
{
Move();
}
private void Move()
{//qui è dove una volta nato conlo faccio muovere
if (waypointIndex <= waypointsMissile.Count - 1)
{
var targetPosition = waypointsMissile[waypointIndex].transform.position + startingPosition; ; //posizione target
var movementThisFrame = 2f * Time.deltaTime; //velocità con la quale si muoverà
transform.position = Vector2.MoveTowards //funzione per muoversi VERSO una posizione target
(transform.position,
targetPosition,
movementThisFrame);
if (transform.position == targetPosition)
{
waypointIndex++;
}
} //se sono all'ultimo
}
}
So this is the current right code, something changed, now when i move and shoot the trajectory is somehow linked on where i am but it is still slightly wrong, like now it’s like every waypoint is moved down/right from the player
at least it is linked to where i am now
Maybe the path is “wrong”. In the scene view, place a shooter. Then set the path parent position to the position of the shooter. If the waypoints are off, move the waypoints to the desired position.
look i found out what is going on, i found out that the pathMissile gameobject which contains the waypoints when placed on unity is like this
now if i place the pathmissile container on the center of the player i get this
WHICH IS THE EXACT behavior of the trajectory i get
so i tried to change the position of the waypoint with the pathMissile standing in the center of the player and it all worked, but WHY? i don’t like this how can i avoid this behavior?
why is it affected by the pathmissile position what if i have a bigger sprite and i don’t know exactly where the center of the player is, i don’t like to put the path missile “manually” at the center and then manipulate the waypoints, i would like a more dinamic approach
i think it’s because it is like we are on coordinates of a graph but mh, correct me if i am wrong but the other time i used move Toward this didn’t happen because i got the Object to spawn exactly on the first waypoint while here we have the projectiles moving from my player right? it is a different usage of em so i don’t think i can make comparison to the spawning and pathing of the enemies and boss
when i say cointainer i refer to this
maybe this happens due to the localPosition thing right?
Your algorithm works as I imagined it. The projectile is supposed to start at the startingPosition
, then it should move toward the first waypoint. That’s exactly what your screenshots show. And now move the waypoints up and leave the parent game object where it is.
If you find the idea difficult to grasp: Do you know those parabola stencils? You take that thing and move it around to draw parabolas. You can reuse it in different places. The parabola always looks the same, no matter where you draw it or whether you rotate the stencil.
You do the same here. Your path game object is the “parabola stencil”. You reuse it at multiple positions.
if i leave the container of the waypoints at the 0,0,0 coordinates it makes the shooting behave not with the path i want, to have the trajectory i imagine i have to put the PathMissile on the center of the player
The position of the parent is completely irrelevant. Only the position of the children relative to the parent matter.
so i have to manually put the container where i first instantiate the player object? what if i let’s say move by mistake the player object, it will mess up with everything
Yeah but did you understand what is going on now ? the path is dependent on where the Container of the waypoints, the prefab is instantiated in unity
or likely it is dependant on where the center of the player is, so to get it right i have to put the center of the container to match with the center of the player and then manipulate the waypoints manually
maybe i’ll make it a child of player so when i reset it , it gets to the center, but again does this happen because we set it as LocalPosition?
You need some reference point (pivot point). Otherwise, the children would not have a position. Everything is relative to something else. That’s how “the world” works. Maybe you’ve heard about that guy named Albert Einstein.
You are creating a path template here. And since there is no such thing as a coordinate without a reference point, you need a root object. Yours is “Path Missile”.
However, we are only interested in the relative position of the children because we want to move the path around just like the parabola stencil. Relative to the pivot point, but not to a specific pivot point with a predefined position.
In your screenshot, the relative position of the first waypoint is below the pivot point. It does not matter if you move the “Path Missile” left or right, up or down. The first waypoint will always be below the pivot unless you move the waypoint up.
ok i got it all, with clarity
–i managed to get the behavior i wanted, now only two things remains haha one is a problem i figured out only now by changing the sprite and it is the rotation of the object , which i would like to face toward the waypoints, so up, i know i could just change the rotation of the image , but i was wondering if i could dinamically change the rotation for each waypoint, it would be really cool and super useful in future projects too.
–Last thing is that now i need to understand how to get the damage go to a “radious” like an exploding bomb and not damaging only the enemy who has been hit, but also who surround him
–(lastly a little curiosity by me, is there a way to get like in automatic the waypoint for a super precise parabola? like implementing in some way the mathematic formulas for it? i know this is a bit more complicated subject so you can also not answer this)
Awesome! Your video looks really great. Maybe you could add two more waypoints to smooth the parabola a bit but apart from that, it looks fantastic. And the good thing is that you have a flexible solution if you wanted to add other path shapes.
For future reference, just type your idea into your favourite search engine. It is very likely that people in the path had the same idea as you and developed a solution for “your” problem.
Maybe scaling the transform with a coroutine? Or an animation?
Sure. Create a parabola function to generate your waypoints, then rotate the imaginary path object with a rotation matrix. Alternatively, you could put everything into one line and generate the path while the projectile is moving, but I would rather opt for the “path generator” because at some point, the player will not notice anymore whether there are 100 waypoints on the path or 1000.
This is how an example output could look like:
Vector3[] path = new Vector3[] {
new Vector3(0f, 0f, 0f),
new Vector3(1f, 1f, 0f),
new Vector3(2f, 2f, 0f)
};
Thank you kindly!
Mhh so how could i implement the parabola function maybe i could take from the waypoint list only the starting point and the “landing” point, and calculate the point in between with a formula, but how could that take place in my code?
For the explosion damage i found this video GRENADE / BOMB in Unity (Tutorial) - YouTube
do you think this could help?
You will have to develop an algorithm to solve this problem. The quadratic function has got different parameters. You need three things to calculate the path: startingPoint
, endPoint
and vertex
.
If you haven’t seen this before, watch a few videos on Youtube, or, if you want to dive deeper into maths, enrol in Ben’s and Gary’s maths course.
You are in the Laser Defender section, which means that you already have some experiencing in game developing. If that video looks promising to you, give it a go. Be a game developer!