- Home /
2D Object Movement, PingPong issues
I'm working on a Top-Down 2D, Raiden Style Shooter. The player Ship basically stays withing a clamped range, and doesn't move through game space. Enemy objects spawn off the screen, then move down to a random 'near' range before they start firing/moving.
I have it set up to have the enemies pingpong when they hit the near range, however, for some reason the enemies jump position at the start of it, leading to a choppy transition between their approach and the start of their patterns. Here's the code - tell me what you think is wrong please.
enum enemyRange {Far =0, Near =1}
var positionSet: boolean;
//var player : Transform;
var initialPosition : Vector3;
var enemyWiggle : Vector2;
var nearRange : float;
static var eRange = enemyRange.Far;
var enemySpeed : float;
function Awake() {
nearRange = Random.Range(3,4);
}
function Start () {
}
function Update () {
if (!positionSet) {
CheckStatus();
}
if (positionSet) {
EnemyPong();
}
//Initiates the range testing
switch (eRange) {
case enemyRange.Near:
break;
case enemyRange.Far:
transform.Translate(-Vector3.forward*(enemySpeed)*Time.deltaTime);
break;
}
}
function CheckStatus()
{
if ((transform.position.z > nearRange)&&(!positionSet)){
eRange = enemyRange.Far;
positionSet = false;
}
else if (((transform.position.z <= nearRange) && (eRange==enemyRange.Far)&&(!positionSet))){
//print (transform.position.z + "should be where it pings to");
positionSet = true;
eRange = enemyRange.Near;
initialPosition = transform.position;
}
}
function EnemyPong() {
transform.position.x = Mathf.Clamp(initialPosition.x + Mathf.PingPong(Time.time * enemySpeed, enemyWiggle.x),-4,4);
transform.position.z = Mathf.Clamp(initialPosition.z + Mathf.PingPong(Time.time * enemySpeed, enemyWiggle.y), 1,49);
}
Your answer

Follow this Question
Related Questions
Creating a 2D Movement Grid 0 Answers
Need air control for 2D-Game 0 Answers
How do you make a 2D char. dodge and move to his original spot? 0 Answers
Issues with Player Input System & 2D,Issues with Player Input system & 2D Movement 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers