• 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 robicro · Jun 04, 2017 at 02:01 PM · animationanimationclipclick to move

Animation wont play (click to move)

 using UnityEngine;
 using System.Collections;
 
 public class ClickToMove : MonoBehaviour {
     private Transform myTransform;                // this transform
     private Vector3 destinationPosition;        // The destination Point
     private float destinationDistance;            // The distance between myTransform and destinationPosition
 
     public float moveSpeed;                        // The Speed the character will move
 
     public AnimationClip idle;
     public AnimationClip run;
     public AnimationClip punch;
 
 
 
     void Start () {
         myTransform = transform;                            // sets myTransform to this GameObject.transform
         destinationPosition = myTransform.position;            // prevents myTransform reset
 
     }
 
     void Update () {
 
 
             if (Input.GetKeyDown (KeyCode.X)) {
                 GetComponent<Animation> ().Play (punch.name);
 
             }
         
 
 
         // keep track of the distance between this gameObject and destinationPosition
         destinationDistance = Vector3.Distance(destinationPosition, myTransform.position);
 
         if (destinationDistance < .5f) {        // To prevent shakin behavior when near destination
             moveSpeed = 0;
             GetComponent<Animation> ().Play (idle.name);
         } else if (destinationDistance > .5f) {            // To Reset Speed to default
             moveSpeed = 30;
             GetComponent<Animation> ().Play (run.name);
         } 
 
 
 
 
         // Moves the Player if the Left Mouse Button was clicked
         if (Input.GetMouseButtonDown(0)&& GUIUtility.hotControl ==0) {
 
             Plane playerPlane = new Plane(Vector3.up, myTransform.position);
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             float hitdist = 0.0f;
 
             if (playerPlane.Raycast(ray, out hitdist)) {
                 Vector3 targetPoint = ray.GetPoint(hitdist);
                 destinationPosition = ray.GetPoint(hitdist);
                 Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
                 myTransform.rotation = targetRotation;
             }
 
 
         }
 
 
         // Moves the player if the mouse button is hold down
         else if (Input.GetMouseButton(0)&& GUIUtility.hotControl ==0) {
 
             Plane playerPlane = new Plane(Vector3.up, myTransform.position);
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             float hitdist = 0.0f;
 
             if (playerPlane.Raycast(ray, out hitdist)) {
                 Vector3 targetPoint = ray.GetPoint(hitdist);
                 destinationPosition = ray.GetPoint(hitdist);
                 Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
                 myTransform.rotation = targetRotation;
             }
             //    myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
         }
 
         // To prevent code from running if not needed
         if (destinationDistance > .5f) {
             myTransform.position = Vector3.MoveTowards (myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
 
 
         } 
 
     }
 
 
 
 
 }


hi, idle and run animations Works fine.. but i want play punch animation when i press X... and not wokring.. i dont understans...

sory for my engrish.. help me pls

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 robicro · Jun 04, 2017 at 03:30 PM

Help me plsss

Comment
Add comment · Show 1 · 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
avatar image ApesUpt · Jun 04, 2017 at 03:54 PM 0
Share

i dont know if this is the issue, but ins$$anonymous$$d of doing GetComponent () .Play i would make declare a variable anim and in the Start function put anim = Getcomponent() then just do anim.Play() whenever you want to play it. This is just syntax, not actually your error (probably)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

199 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 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Walking animation on a click to move script 1 Answer

[2D] Play animation backward. 1 Answer

How to add clips to my 3D model ? 0 Answers

change player position when animation in played 0 Answers

Why isn't my animation looping? 3 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