Using script rather than animator controller

I am trying to follow this

tutorial but i decieded to use my own animations so i followed this

except it uses the animator controller rather than script, and i dont know how to convert the script and it will probably cause problems later if i do, so i was wondering how to use script instead of the controller?
Here is the java script

#pragma strict

private var hasAxe : boolean = false;

private var canSwing : boolean = true;
private var isSwinging : boolean = false;
var swingTimer : float = 0.7;

private var controller : CharacterController;
private var playerGUI : PlayerGUI;

function Start()
{
hasAxe = true;
controller = GameObject.Find(“First Person Controller”).GetComponent(CharacterController);
playerGUI = GameObject.Find(“First Person Controller”).GetComponent(PlayerGUI);
}

function Update()
{
//If we aren’t moving and if we aren’t swinging, then we idle!

if(controller.velocity.magnitude <= 0 && isSwinging == false)
{
GetComponent.().Play(“Idle”);
GetComponent.()[“Idle”].wrapMode = WrapMode.Loop;
GetComponent.()[“Idle”].speed = 0.2;
}

//If we’re holding shift and moving, then sprint!

if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
{
GetComponent.().Play(“Sprint”);
GetComponent.()[“Sprint”].wrapMode = WrapMode.Loop;
}

//WOODCUTTING SECTION
if(hasAxe == true && canSwing == true)
{
if(Input.GetMouseButtonDown(0))
{
//Stamina reduction applied to the PlayerGUI script
playerGUI.staminaBarDisplay -= 0.1;

//Swinging animation
GetComponent.().Play(“Swing”);
GetComponent.()[“Swing”].speed = 2;
isSwinging = true;
canSwing = false;
}
}

if(canSwing == false)
{
swingTimer -= Time.deltaTime;
}

if(swingTimer <= 0)
{
swingTimer = 1;
canSwing = true;
isSwinging = false;
}
}

The first mentioned tutorial is outdated in terms of the new Mecanim animation system. I assume you are new to Unity and therefor it might be very challenging to do script based animation with little coding experience. I suggest that you do a newer tutorial that contains no animations at all, for example how to roll a ball: Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn

Bonus tip: You can format your code in the forum with code tags [ CODE ] write something here [ /CODE ] (no spaces)

 write something here

I have done that tutorial and have a basic understanding of unity please try to answer the question, i am just trying to get it to work, also thanks for the bonus tip

Ok, I have to correct myself. The first tuturial is somehow up-to-date and I assumed it was the legacy system because it did not make use of the animator. It handles all the animation transitions by code instead of using an Animator (which I personally like the most about Mecanim). The Animation and the Animator go side by side, it’s not exclusive. The “if else”-blocks in your code can be simulated by Animator nodes and transitions, which makes it much easier to keep overview.

Back to the question. How are your animations different to the ones in the 1st tutorial? Just different names? Completely different behaviour? Less/more bones? Not even humanoid? Did you check the input settings? Can you preview the animations?

Did you assign the Animation component and your script to the same game object? Did you drag all the animation assets to the animations array in the Animation component? Are they all named correctly like you want to call them from script?

When i use the script the animations dont play at all, similarly if i put it on auto play, but in the animator controller it does play them, i dont know how they differ as the animations it uses are just made beforehand and not in a video, the animations are under the rig, as clips so that might be the problem?

Are you refering to his clip? Unity - Manual: Animation tab

The yes, you might have to replace the AnimationClip with the Animation component. Could you provide a screenshot of the gameobject showing the attached components?

Edit: There should be errors like missing references when you try to run your code at the moment.

There sorry for the late reply completely forgot

2117732--139072--Capture.PNG

Haha ok, so you are still stuck :wink: I meant the gameobject in the inspector, not the animation asset.

didn’t no which one you meant

In your attached 2nd image , under the “Rig” tab, change from “Generic” to “Legacy” if you want to use Animation instead of Animator(and Animator Controller).

cheers will try that tommorow

Where do you reference your animations in the scene? It has to be somehow connected to a GameObject like the character. The GameObject looks like the first image and in your case it should have an Animation component like the second image.


Ok i thought i posted this yesterday but it must of not worked. So i changed it to legacy and it worked yay, but now when it plays the animation for attack it doesn’t finish sometimes. Any clues?

it depends on how you use/check the animations, but for non looping animation, I tend to use
WrapMode.ClampForever