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();
}
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();
facingRight = true;
anim = GetComponent();
}
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!
↧