- Home /
Why does a rigidbody won't move on my EnemyMovement class?
This is my EnemyMovement.cs
public class EnemyMovement{
public const int MAX_SWAP_DIRECTION_DURATION = 2;
public const int MAX_WALK_DURATION = 15;
public static string[] HORIZONTAL_MOVEMENT = new string[] {"LEFT", "RIGHT"};
public static string[] VERTICAL_MOVEMENT = new string[] {"TOP", "BOTTOM"};
private float walkSpeed, jumpSpeed, walkDur, swapDirDur;
private string side, dir;
private int movStat;
private bool jumpStatus;
public EnemyMovement(){}
public void SetCurrentSide(string side){
this.side = side;
}
public string GetCurrentSide(){
return side;
}
public void SetCurrentDirection(string dir){
this.dir = dir;
}
public string GetCurrentDirection(){
return dir;
}
public void SetWalkSpeed(float walkSpeed){
this.walkSpeed = walkSpeed;
}
public float GetWalkSpeed(){
return walkSpeed;
}
public void SetJumpSpeed(float jumpSpeed){
this.jumpSpeed = jumpSpeed;
}
public float GetJumpSpeed(){
return jumpSpeed;
}
public void SetWalkDur(float walkDur){
this.walkDur = walkDur;
}
public float GetWalkDur(){
return walkDur;
}
public void SetSwapDirDur(float swapDirDur){
this.swapDirDur = swapDirDur;
}
public float GetSwapDirDur(){
return swapDirDur;
}
public void SetJumpStatus(bool jumpStatus){
this.jumpStatus = jumpStatus;
}
public bool IsJump(){
return jumpStatus;
}
public void Jump (GameObject ball, Rigidbody2D rBody, Transform transform){
Vector3 targetdir = ball.transform.position - transform.position;
transform.rotation= Quaternion.LookRotation(Vector3.forward,targetdir);
rBody.AddForce(transform.up * jumpSpeed * Time.deltaTime, ForceMode2D.Impulse);
transform.eulerAngles = new Vector3(0, 0, 0);
rBody.constraints = RigidbodyConstraints2D.FreezeRotation;
}
public void Move(Rigidbody2D rBody, Transform transform){
walkDur -= Time.deltaTime;
swapDirDur -= Time.deltaTime;
if(swapDirDur <= 0)
swapDirDur = Random.Range (0, MAX_SWAP_DIRECTION_DURATION);
if (walkDur <= 0) {
jumpStatus = true;
walkDur = Random.Range (0, MAX_WALK_DURATION);
}
switch (side) {
case "Top":
Movement ("Horizontal", rBody, transform);
break;
case "Bottom":
Movement ("Horizontal", rBody, transform);
break;
case "Right":
Movement ("Vertical", rBody, transform);
break;
case "Left":
Movement ("Vertical", rBody, transform);
break;
}
}
private void Movement(string direction, Rigidbody2D rBody, Transform transform){
switch(direction){
case "Horizontal":
rBody.constraints = RigidbodyConstraints2D.None;
rBody.constraints = RigidbodyConstraints2D.FreezePositionY | RigidbodyConstraints2D.FreezeRotation;
if (swapDirDur <= 0.0f) {
swapDirDur = Random.Range (0, MAX_SWAP_DIRECTION_DURATION);
movStat = Random.Range (0, HORIZONTAL_MOVEMENT.Length);
} else {
string horMov = HORIZONTAL_MOVEMENT [movStat];
dir = horMov;
switch (horMov) {
case "LEFT":
//rBody.velocity = -transform.right * speed * Time.fixedDeltaTime;
rBody.MovePosition (transform.position - transform.right * walkSpeed * Time.fixedDeltaTime);
break;
case "RIGHT":
//rBody.velocity = transform.right * speed * Time.fixedDeltaTime;
rBody.MovePosition (transform.position + transform.right * walkSpeed * Time.fixedDeltaTime);
break;
}
}
break;
case "Vertical":
rBody.constraints = RigidbodyConstraints2D.None;
rBody.constraints = RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezeRotation;
if (swapDirDur <= 0.0f) {
swapDirDur = Random.Range (0, MAX_SWAP_DIRECTION_DURATION);
movStat = Random.Range (0, VERTICAL_MOVEMENT.Length);
} else {
string verMov = VERTICAL_MOVEMENT [movStat];
dir = verMov;
switch (verMov) {
case "TOP":
rBody.MovePosition (transform.position + transform.up * walkSpeed * Time.fixedDeltaTime);
break;
case "BOTTOM":
rBody.MovePosition (transform.position - transform.up * walkSpeed * Time.fixedDeltaTime);
break;
}
}
break;
}
}
And this is my enemy script that attach to then enemy object.
public class Enemy : MonoBehaviour {
private EnemyMovement movement;
private Rigidbody2D rBody;
public GameObject ball;
//Temporary
private Text walk;
private Text swap;
private Text x;
private Text y;
private Text z;
private Text jump;
private Text direction;
private Text sides;
private Text speeds;
// Use this for initialization
void Start () {
rBody = GetComponent<Rigidbody2D> ();
movement = new EnemyMovement ();
movement.SetWalkDur (Random.Range (0, EnemyMovement.MAX_WALK_DURATION));
movement.SetSwapDirDur (Random.Range (0, EnemyMovement.MAX_SWAP_DIRECTION_DURATION));
//Temp
walk = GameObject.Find ("Walk").GetComponent<Text> ();
swap = GameObject.Find ("Swap").GetComponent<Text> ();
x = GameObject.Find ("X").GetComponent<Text> ();
y = GameObject.Find ("Y").GetComponent<Text> ();
z = GameObject.Find ("Z").GetComponent<Text> ();
jump = GameObject.Find ("Jump").GetComponent<Text> ();
direction = GameObject.Find ("Direction").GetComponent<Text> ();
sides = GameObject.Find ("Side").GetComponent<Text> ();
speeds = GameObject.Find ("Speed").GetComponent<Text> ();
}
// Update is called once per frame
void FixedUpdate () {
if (movement.IsJump()) {
movement.Jump (ball, rBody, transform);
} else {
movement.Move (rBody, transform);
}
walk.text = "WalkDur: " + movement.GetWalkDur();
swap.text = "SwapDur: " + movement.GetSwapDirDur();
x.text = "X: " + transform.position.x;
y.text = "Y: " + transform.position.y;
z.text = "Z: " + transform.position.z;
jump.text = "Jump Status: " + movement.IsJump();
direction.text = "Direction: " + movement.GetCurrentDirection();
sides.text = "Sides: " + movement.GetCurrentSide();
speeds.text = "Speed: " + movement.GetWalkSpeed();
}
void OnCollisionEnter2D(Collision2D col){
transform.eulerAngles = new Vector3(0, 0, 0);
movement.SetCurrentSide(col.gameObject.name);
if (movement.IsJump ())
movement.SetJumpStatus (false);
rBody.gravityScale = 0;
}
}
Please help me. I just passed the value of rigidbody and transform on a move method but it didn't work.
Comment