- Home /
Quick question
I'm pretty new to Unity and Programming as a whole, recently I've been trying to practice with simple animation (just switching idle to walk). I've gotten it to work in js but I'd prefer all my code to be in C#, so I'm curious how it would look.
In Java:
#pragma strict
var speed : float = 5f;
var anim : Animator;
function Start () {
anim = gameObject.GetComponent(Animator);
}
function Update () {
if(Input.GetKeyDown(KeyCode.T))
{
anim.SetFloat("Trigger",1); //Starts walk animation
}
else
{
anim.SetFloat("Trigger",0); //Otherwise loop idle animation
}
}
What I tried:
using UnityEngine;
using System.Collections;
public class Testing : MonoBehaviour {
private float speed = 5f;
private Animator anim;
void Start () {
anim = gameObject.GetComponent(Animator);
}
void Update () {
if(Input.GetKeyDown(KeyCode.T))
{
anim.SetFloat("Trigger",1); //Starts walk animation
}
else
{
anim.SetFloat("Trigger",0); //Otherwise loop idle animation
}
}
}
It's not working, but again I'm just starting to learn to program. What should it look like in C#?
Answer by robertbu · Feb 15, 2014 at 05:33 AM
The only problem I see is line 11. A C# conversion would be:
anim = gameObject.GetComponent<Animator>();
Your answer
Follow this Question
Related Questions
Printing text to Console & Getting User Input 2 Answers
Trigger animation problem 0 Answers
How do You Scale a Character/Object up and down? 2 Answers
How to reference a public gameobjects in script which are attached in the inspector. (solved) 2 Answers
Can the animation editor create local rotational data? 3 Answers