#Share the entire script.
1 messages · Page 1 of 1 (latest)
public float moveSpeed;
public float originalMoveSpeed;
public Transform nextPoint;
public int travelDistance = 0;
public int whenToStart = 500;
[Header("Points")]
public Transform start;
public Transform point1;
public Transform point2;
public Transform point3;
public Transform point4;
public Transform point5;
public Transform point6;
public Transform end;
public void Update()
{
if (whenToStart <= 0)
{
if (transform.parent.name == "onScreenEnemies")
{
transform.position = Vector3.Lerp(transform.position, nextPoint.position, moveSpeed * Time.deltaTime);
}
}
}
public void FixedUpdate()
{
if (whenToStart >= 1)
{
whenToStart -= 1;
}
if (transform.parent.name == "onScreenEnemies")
{
if (whenToStart <= 0)
{
travelDistance += 1;
}
}
}
public void OnTriggerStay2D(Collider2D collision)
{
if (collision.transform.tag == "Point")
{
if (Mathf.Abs(transform.position.x - nextPoint.position.x) <= 0.001f && Mathf.Abs(transform.position.y - nextPoint.position.y) <= 0.001f)
{
if (collision.transform.name == "point1")
{
nextPoint = point2;
}
else if (collision.transform.name == "point2")
{
nextPoint = point3;
}
else if (collision.transform.name == "point3")
{
nextPoint = point4;
}
else if (collision.transform.name == "point4")
{
nextPoint = point5;
}
else if (collision.transform.name == "point5")
{
nextPoint = point6;
}
else if (collision.transform.name == "point6")
{
nextPoint = end;
}
else if (collision.transform.name == "end")
{
if (transform.parent.name == "onScreenEnemies")
{
gameVariables.GetComponent<waveSystem>().EnemyDefeated();
}
gameVariables.GetComponent<gameVariables>().currentGameHealth -= transform.GetChild(0).GetComponent<health>().currentHealthAmount;
Destroy(gameObject);
}
}
}
}
thats all the main stuff in the movement script
please use one of the paste sites linked in !code for large amounts of code like this
📃 Large Code Blocks
Use links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/, https://paste.myst.rs/, https://hastebin.com/
📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
(and paste the entire script)
use https://gdl.space .
paste your code into the site and click the save icon, then copy the resulting link
Ok, some stuff in the code is for other use, but here is the whole entire enemyMovement script.
Note: Enemies that spawn do not get affected by this "slow moving" thing. It's only when I load a saved game, that enemy gets cloned back into its position and when its near their nextPoint, the movement is slower
You can watch this is slow mode to see that the closer those enemies are to that corner, the slower they were, until they hit that nextPoint, they all went back to normal speed
yes
ok
public void Update()
{
if (whenToStart <= 0)
{
if (transform.parent.name == "onScreenEnemies")
{
transform.position = Vector3.Lerp(transform.position, nextPoint.position, moveSpeed * Time.deltaTime);
}
}
}
you're using Vector3.Lerp here
Vector3.MoveTowards isn't anywhere in this script
sorry, its supposed to be vector3.movetowards. i was trying new things
this is what i meant: transform.position = Vector3.MoveTowards(transform.position, nextPoint.position, moveSpeed * Time.deltaTime);
im failing so bad at sharing this code
make sure you're still getting that behavior when using MoveTowards.
if you are, try commenting that out entirely and see if the enemies move at all
perhaps something else is interfering
the void update func?
no, just that single line of code.
okay, so the MoveTowards code should be fine in isolation
do the enemies have non-kinematic rigidbodies?
all enemies have this same 2D rigid body
i tried freezing both x and y, they still moved but the slowing down problem persisted
It’s dynamic, so it will be affected by physics forces. Consider making it kinematic.
OH MY GOSH. ITS BECAUSE THE Z VALUE GETS CHANGED EVERY TIME THE SAVED LOADED CLONE GETS PUT IN!!!! AFTER 3 HOURS, (thank you so much btw)
explanation: the normal Z value is supposed to be 1. when i saved a game and loaded it back in, the Z value of that clone went to a random high number like 147 or something. the reason we couldnt see the movement is because were looking at the game from the x and y perspective. vector3.movetowards is working perfectly and its moving at a constant speed, its just that the enemy was trying to get the same value of Z as its nextPoint!
that was brain killing man.. thank you so much for taking time to help me tho!
hopefully that made sense
ah, that would do it
Turning off 2D mode can help you spot that kind of thing!
I didn’t think of that at all
It was a tricky error that was hard to see. Thank you so much for your time though! I do really appreciate it