Quantcast
Channel: Questions in topic: "javascipt"
Viewing all articles
Browse latest Browse all 1875

2D Enemy patrol using javascript

$
0
0
I have a javascript code for enemy patrol that I translated from C#. I am not flagging any errors from it, but my enemy flips back and forward rapidly and does not patrol. basically using a linecast to detect the edge of a platform and flip once the edge is detected. any help would be awesome! javascript code #pragma strict public var speed : float; public var enemyMask : LayerMask; private var myBody : Rigidbody2D; private var myTrans : Transform; private var myWidth : float; private var lineCastPos : Vector2; private var isGrounded : boolean; private var currRot : Vector3; function Start () { myTrans = this.transform; myBody = this.GetComponent(Rigidbody2D); myWidth = this.GetComponent(SpriteRenderer).bounds.extents.x; } function FixedUpdate () { var lineCastPos : Vector2 = myTrans.position - myTrans.right * myWidth; var isGrounded : boolean = Physics2D.Linecast (lineCastPos, lineCastPos + Vector2.down, enemyMask); if (!isGrounded) { currRot = myTrans.eulerAngles; currRot.y += 180; myTrans.eulerAngles = currRot; } var myVel : Vector2 = myBody.velocity; myVel.x = -myTrans.right.x * speed; myBody.velocity = myVel; } c# code using UnityEngine; using System.Collections; public class Enemytest : MonoBehaviour { public float speed; public LayerMask enemyMask; Rigidbody2D myBody; Transform myTrans; float myWidth; // Use this for initialization void Start () { myTrans = this.transform; myBody = this.GetComponent (); myWidth = this.GetComponent ().bounds.extents.x; } // Update is called once per frame void FixedUpdate () { Vector2 lineCastPos = myTrans.position - myTrans.right * myWidth; bool isGrounded = Physics2D.Linecast (lineCastPos, lineCastPos + Vector2.down, enemyMask); if (!isGrounded) { Vector3 currRot = myTrans.eulerAngles; currRot.y += 180; myTrans.eulerAngles = currRot; } Vector2 myVel = myBody.velocity; myVel.x = -myTrans.right.x * speed; myBody.velocity = myVel; } } the C# code works perfectly

Viewing all articles
Browse latest Browse all 1875

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>