Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Creative Inventory · Aug 27, 2015 at 02:40 PM · animationmovementanimatorplayer

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);
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Official Video

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges