[SOLVED] Moving the enemy formation (lecture 109) Ships vibrate back and forth

this is the code im using on the enemy spawner…as far as i can see my code is exactly like the course but it just makes my ships go back and forth so quickly they just kind of vibrate…when i turned the speed down i could see them move very briefly side to side…
any ideas…??

using UnityEngine;
using System.Collections;

public class EnemySpawner : MonoBehaviour {
public GameObject enemyPrefab;
public float width = 10f;
public float height = 5f;
public float speed = 5f;

private bool movingRight = true;
private float xmax;
private float xmin;

// Use this for initialization
void Start () {
	float distanceToCamera = transform.position.z - Camera.main.transform.position.z;
	Vector3 leftEdge = Camera.main.ViewportToWorldPoint(new Vector3(0,0, distanceToCamera));
	Vector3 rightEdge = Camera.main.ViewportToWorldPoint(new Vector3(1,0, distanceToCamera));
	xmax = rightEdge.x;
	xmin = leftEdge.x;
	foreach(Transform child in transform) {
		GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
		enemy.transform.parent = child;
	}
}


public void OnDrawGizmos(){
	Gizmos.DrawWireCube (transform.position, new Vector3 (width,height));

}


// Update is called once per frame
void Update () {
	if (movingRight){
		transform.position += Vector3.right * speed *Time.deltaTime;
	}else{
		transform.position += Vector3.left * speed*Time.deltaTime;
	}
	float rightEdgeOfFormation = transform.position.x +(0.5f*width);
	float leftEdgeOfFormation = transform.position.x - (0.5f *width);
	if (leftEdgeOfFormation < xmin || rightEdgeOfFormation >xmax);{
		movingRight = !movingRight;
	}
}

}

Hi,

Have you already watched lecture 110. Fixing The Formation Movement?

1 Like

no i havnt :slight_smile:

ok im looking at this now and it IS vibrating kinda like what is shown there but mine were never actually moving back and forth properly…changing the speed never affected it doing this…

Ok so ive found that my camera wasnt big enough for the scene and have changed it but it is still vibrating back and forth in the center of the screen

I also get an error saying a possible mistaken empty entry on the line of code with the || right at the bottom

Hi Blake,

there is an extraneous semicolon at the end of your last if statement:

	if (leftEdgeOfFormation < xmin || rightEdgeOfFormation >xmax);{
		movingRight = !movingRight;
	}

Remove the semicolon, and the script should work as intended :wink:

2 Likes

Thanks for that dude that seems to have fixed it…
One last thing…it is now moving correctly but its a bit jerky is there anyway to fix this? in the video brice’s ships arent jerky at all

You could give Vector3.SmoothDamp a try.

1 Like

thanks! :smiley: but how do i use this…??? where does it fit into the code?

Well, actually it’s your job to figure that out. :wink:

You could start with replacing Time.deltaTime by Time.smoothDeltaTime.

If that does not look good, you could replace transform.position += (rest of your code) by Transform.translate. Or Vector3.Lerp.

Or you could use Rigidbody2D.AddForce in FixedUpdate(). That’s probably the easiest solution apart from Time.smoothDeltaTime.

There are multiple ways of moving game objects around in Unity. In most cases you can find examples in the API. Just try out what works best for you.

1 Like

i will try that thankyou
to be honest though working it out by myself seems almost impossible…like learning chinese from a chinese dictionary with no context to understand the language it could take ten years by trial and error…or may never be understood. Its like trying to fix a car when you dont understand what anything inside of it does…can only learn through constantly repeating it over and over within context

I understand what you mean.

Perhaps it helps if you regard C# and Unity as two different things. In your scripts you manipulate variables like position, and when you do that, something happens in Unity. We don’t know how exactly Unity works because it’s not open source. However, we know when we write this or that in our script, we (hopefully) get the expected result.

The API is our most important tool. When taking a look at Transform.position, you’ll notice that it’s a Vector3. Hence you can do stuff with Vector3 and assign it to position. All you need to do is some maths.

Another thing to know about Unity is Components. Most variables you see in your Inspector can be changed via code. For example, when you attach a Rigidbody2D component, you can search the API for relevant methods or properties.

Perhaps it also helps to learn more about C#. In this course you learn it along with Unity and game development, so it’s a rather slow process. If you are interested in C#, though, you could take a look at Mosh Hamedani’s C# course. Here is a free preview covering a few topics.

1 Like

thanks :slight_smile:

Privacy & Terms