- Home /
The question is answered, right answer was accepted
Need Help with Enemy Attack Animation Script? I'm new to Unity.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy1Animation : MonoBehaviour
{
Animator anim;
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Player")
{
collided = true;
}
else
{
collided = false;
}
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (collided = true)
{
anim.SetInteger("State", 1);
}
}
}
}
So this is the code I have- In my animator controller I have an Int Parameter called "State" and conditions on the transitions between walking and attacking in which State must = 0 for walk animation and 1 for attack animation. The image included shows the errors I got after attaching the script to an enemy prefab which also has an Animator Component with the correct Animator Controller. I'm very new to Unity and was relying on a tutorial for Animating my enemies but I can't figure out what I've done to cause these errors- any help would be really appreciated, sorry if this is a really simple thing that I should already know. Like I said, I'm very new to Unity.
Answer by bhavinbhai2707 · Jan 18, 2018 at 02:17 AM
You added an extra } (curly braces) at the end, remove that.
The if statement in update method , you are assigning the variable and not comparing , so in order to do that correctly write it with ==
If(collided == false) { //Statements }
Thank you!! I forgot the extra = and had a curly brace going to wrong line of text. That fixed it!
always there to help mate!! keep learning :)