Question by
KreeperGaming · Sep 19, 2016 at 11:46 PM ·
2daienemyplatformerenemyai
2D Enemy Ai
I just can't get it to work. At all.
he just shrinks into the ground, and he just shakes. It hurts my eyes too.
Enemy AI Script
using UnityEngine;
using System.Collections;
public class Character : MonoBehaviour {
[SerializeField]
private GameObject player;
private Rigidbody2D body;
public Vector2 velocity;
void Start()
{
body = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
if(transform.position.x > player.transform.position.x)
{
body.velocity = velocity * -1;
}
else if (transform.position.x < player.transform.position.x)
{
body.velocity = velocity;
}
else
{
body.velocity = new Vector2(0, 0);
}
}
}
Player Script, if you need it
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
[SerializeField]
private float movementSpeed = 5;
private bool attack;
private bool slide;
private Rigidbody2D myRigidBody;
private Animator anim;
private bool facingRight;
[SerializeField]
private Transform[] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
private bool isGrounded;
private bool jump;
private bool jumpAttack;
[SerializeField]
private bool airControl;
[SerializeField]
private float jumpForce;
// Use this for initialization
void Start ()
{
myRigidBody = GetComponent<Rigidbody2D>();
facingRight = true;
anim = GetComponent<Animator>();
}
void Update() //Check everthing in frames
{
HangleInput();
}
// Update is called once per frame
void FixedUpdate () //Check Everything in seconds
{
float horizontal = Input.GetAxis("Horizontal");
isGrounded = IsGrounded();
HandleMovement(horizontal);
Flip(horizontal);
HandleAttacks();
HandleLayers();
ResetValues();
}
private void HandleMovement(float horizontal) // Handles movement
{
if(myRigidBody.velocity.y < 0)
{
anim.SetBool("land", true);
}
if(!anim.GetBool("slide") && (isGrounded || airControl))
{
myRigidBody.velocity = new Vector2 (horizontal * movementSpeed, myRigidBody.velocity.y);
}
anim.SetFloat("speed", Mathf.Abs(horizontal));
if(isGrounded && jump)
{
isGrounded = false;
myRigidBody.AddForce(new Vector2( 0, jumpForce));
anim.SetTrigger("jump");
}
if(slide && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("Slide"))
{
anim.SetBool("slide", true);
}
else if (!this.anim.GetCurrentAnimatorStateInfo(0).IsName("Slide"))
{
anim.SetBool("slide", false);
}
}
private void HandleAttacks() //Handles all attacks
{
if(attack && isGrounded && !this.anim.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
{
anim.SetTrigger("attack");
}
if(jumpAttack && !isGrounded && !this.anim.GetCurrentAnimatorStateInfo(1).IsName("JumpAttack"))
{
anim.SetBool("jumpAttack", true);
}
if(!jumpAttack && !this.anim.GetCurrentAnimatorStateInfo(1).IsName("jumpAttack"))
{
anim.SetBool("jumpAttack", false);
}
}
private void HangleInput() // Handles all buttons pressed
{
if(Input.GetKeyDown(KeyCode.LeftShift) || Input.GetMouseButtonDown(0))
{
attack = true;
jumpAttack = true;
}
if(Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S))
{
slide = true;
}
if(Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
{
jump = true;
}
}
private void Flip (float horizontal) // flips player direction
{
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
private void ResetValues() //resets values to default
{
attack = false;
slide = false;
jump = false;
jumpAttack = false;
}
private bool IsGrounded() // checks if we are grounded
{
if(myRigidBody.velocity.y <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for(int i = 0; i < colliders.Length; i++)
{
if(colliders[i].gameObject != gameObject)
{
anim.ResetTrigger("jump");
anim.SetBool("land", false);
return true;
}
}
}
}
return false;
}
private void HandleLayers()
{
if(!isGrounded)
{
anim.SetLayerWeight(1, 1);
}
else
{
anim.SetLayerWeight(1, 0);
}
}
}
Its long, I know.
Thanks!
Comment
you don't really need your player script in this Question. since your using its transform values. can you include your RigidBody2D settings for that Enemy? that will be very helpful.
I haven't changed the settings. I am still having this problem. Thanks.
Your answer
Follow this Question
Related Questions
AI stops following player. 1 Answer
How can I make a 4 direction animation for an enemy? 1 Answer
Vector 2 only using x (Enemy AI) 1 Answer