How do I add animation to my script?
Okay I know how to do all the animation and using the animator, but i'm having problems with telling my game to use the animation I did ( i.e. telling my script to use the animations i did) This is my script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
Public class Playermovement : MonoBehaviour {
public float speed = 5f;
public Text ScoreText;
public AudioClip coinsound;
private Vector3 target;
private int Score;
void Start ()
{
target = transform.position;
Score = 0;
SetScoreText ();
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
Score = Score + 1;
SetScoreText ();
AudioSource.PlayClipAtPoint (coinsound, transform.position);
}
}
void SetScoreText ()
{
ScoreText.text = "Score: " + Score.ToString ();
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
target.z = transform.position.z;
}
transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
}
void LateUpdate () {
var left = Camera.main.ViewportToWorldPoint (Vector3.zero).x;
var right = Camera.main.ViewportToWorldPoint (Vector3.one).x;
var top = Camera.main.ViewportToWorldPoint (Vector3.zero).y;
var bottom = Camera.main.ViewportToWorldPoint (Vector3.one).y;
float x = transform.position.x, y = transform.position.y;
if (transform.position.x <= left + GetComponent<Renderer> ().bounds.extents.x) {
x = left + GetComponent<Renderer> ().bounds.extents.x;
} else if (transform.position.x >= right - GetComponent<Renderer> ().bounds.extents.x) {
x = right - GetComponent<Renderer> ().bounds.extents.x;
}
if (transform.position.y <= top + GetComponent<Renderer> ().bounds.extents.y) {
y = top + GetComponent<Renderer> ().bounds.extents.y;
} else if (transform.position.y >= bottom - GetComponent<Renderer> ().bounds.extents.y) {
y = bottom - GetComponent<Renderer> ().bounds.extents.y;
}
transform.position = new Vector3 (x, y, transform.position.z);
}
}
Answer by pwego · Aug 27, 2015 at 02:59 PM
This answer assumes you know a good bit.
Animator is for taking advantage of unity's built in animator. The thing with arrows pointing at different animations thing. If you want to do it manually use Add component on gameObject "Animation" then drag your animations into it. Then in code just do:
gameObject.GetComponent<Animation>().Play ("animation exact name");
otherwise you do most of the stuff inside the animator itself. After you make an animation it should add 2 files one being the animation and one leading to its animator. Just use one animator and drag all the animations you want to use into that one animator. You make parameters like isMoving or something like a bool maybe. Then right click an animation inside the animator and make transitions using the parameters. This is all very much better learned watching a video, lemme find one.
Your answer
Follow this Question
Related Questions
Can't seem to move my character. 0 Answers
How to make player rotate 180° and run that direction with mecanim + vice versa? 1 Answer
How do I move two first person shooter hands at once using animation? 0 Answers
Proper way to implement animations 0 Answers
´How to check if 3D player is moving? 0 Answers