Hey!
I’m trying to create a fish enemy and want it to float up and down slightly while moving horizontally. I’ve tried to find it’s position once it is created. With this position I try and keep it at relatively the same height moving it up and down slightly. At the moment it only moves down and not back up again.
//Inspector Variables.
var fish01HorizontalSpeed = 6;
var fish01Position = 0;
var fish01VerticalSpeed = 0.5;
function Start () {
fish01Position = transform.position.y;
}
function Update () {
//Initiates movement to the right on the x axis.
transform.Translate(Vector3.up * fish01HorizontalSpeed * Time.deltaTime);
transform.Translate(Vector3.right * fish01VerticalSpeed * Time.deltaTime);
//Checks for horizontal position of fish and reverse direction.
if (transform.position.x >= 15)
{
fish01HorizontalSpeed = -6;
}
if (transform.position.x <= -15)
{
fish01HorizontalSpeed = 6;
}
//Check for vertical position of fish and reverse direction
if (transform.position.y >= fish01Position + 1)
{
fish01VerticalSpeed = -0.5;
}
if (transform.position.y <= fish01Position + -1)
{
fish01VerticalSpeed = 0.5;
}
}
I think you have your horizontal and vertical switched on the transform.Translate part of your script.
– Lazy08I tested horizontal movement before I added vertical. So this is not the problem.
– Zpesh