Hey guys so I’ve got a lot of help to getting the moving platform to work properly but now I’m having trouble with the zombie randomly teleporting when they’re on the platform. heres the video:
http://tinypic.com/player.php?v=35b791v>&s=9#.V1Djer4rIo8
The code for the zombie is the following:
using UnityEngine;
using System.Collections;
public class EnemyMovement : MonoBehaviour
{
public Transform sightEnd, groundStart, groundEnd, sightBegin, jumpEnd;
public float speed,jumpPower, maxSpeed;
private Rigidbody2D _rb2d;
private bool _avoid,_isGrounded, _isFacingRight, _jumpToAvoid, _canJump;
void Awake()
{
_rb2d = GetComponent<Rigidbody2D>();
}
void Update()
{
int mask1 = 1 << LayerMask.NameToLayer("Ground");
int mask2 = 1 << LayerMask.NameToLayer("Obstacle");
int mask3 = 1 << LayerMask.NameToLayer("Player");
// int combinedMask = mask1 | mask2 | mask3;
Debug.DrawLine(transform.position, sightEnd.position, Color.red);
_isGrounded = Physics2D.Linecast(groundStart.position, groundEnd.position, mask1 | mask2);
_avoid = Physics2D.Linecast(sightBegin.position, sightEnd.position, mask3 | mask2 | mask3);
_jumpToAvoid = Physics2D.Linecast(sightBegin.position, jumpEnd.position, mask1 | mask2);
}
void FixedUpdate()
{
if (_isGrounded)
{
_rb2d.velocity = new Vector2(speed, _rb2d.velocity.y);
_canJump = true;
//=============== when avoid is met, flip =====================
if (_avoid)
{
speed = -speed;
}
}
//=============== flipping =====================
if (speed < 0 && !_isFacingRight)
Flip();
else if (speed > 0 && _isFacingRight)
Flip();
//================= jumping condition ===================
if (_jumpToAvoid && _canJump)
{
Jump();
_canJump = false;
}
}
public void Flip()
{
_isFacingRight = !_isFacingRight;
Vector3 scale = transform.localScale;
scale.x *= -1;
transform.localScale = scale;
}
public void Jump()
{
_rb2d.AddForce(new Vector2(_rb2d.velocity.x,jumpPower));
//_rb2d.velocity = new Vector2(_rb2d.velocity.x, jumpPower);
}
}
And heres the code for the moving platform:
public class PlatformMoveScript : MonoBehaviour
{
public Transform endPosition;
public enum AnimationType {MoveToPos, MoveToPosAndStop};
public AnimationType aniType = AnimationType.MoveToPos;
public float duration;
private Rigidbody2D _current;
void Awake()
{
_current = GetComponent<Rigidbody2D>();
}
void Start ()
{
if(aniType == AnimationType.MoveToPos)
MoveToPos();
}
void MoveToPos()
{
_current.DOMove(endPosition.position, duration, true).SetEase(Ease.Linear).SetLoops(-1, LoopType.Yoyo).SetDelay(0f).SetUpdate(UpdateType.Fixed);
}
void OnTriggerStay2D (Collider2D col)
{
if(col.tag == "Player" || col.tag == "Zombie" || col.tag == "Insulin")
col.transform.parent = this.transform;
//this.transform.SetParent(col.transform,false);
}
void OnTriggerExit2D (Collider2D col)
{
col.transform.parent = null;
}
}