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 nemervore · Nov 30, 2015 at 10:56 AM · c#animationraycastingclick to move

Animating a click to move character

 private Vector3 Position;
 public float Here;
 public float Speed;
 public float RotSpeed;
 public CharacterController Controller;

 //animations

 public AnimationClip ModIdle;
 public AnimationClip ModRun;


 // Use this for initialization
 void Start () {
     Position = transform.position;
 }
 
 // Update is called once per frame
 void Update () {

     if (Input.GetMouseButton (0))

     //locate mouse position
         LocatePosition ();

     //move player to the mouse place
         MovePlayer ();
 
 }
 void LocatePosition()
 {
     RaycastHit hit;
     Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);

     if (Physics.Raycast (ray, out hit, 1000)) {
         Position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
     }
     Debug.Log (Position);
 
 }

 void MovePlayer()
 {
     //when the object is moving
     if (Vector3.Distance (transform.position, Position) > Here) {
         Quaternion NewRotation = Quaternion.LookRotation (Position - transform.position);

         NewRotation.x = 0f;
         NewRotation.z = 0f;

         transform.rotation = Quaternion.Slerp (transform.rotation, NewRotation, Time.deltaTime * RotSpeed);
         Controller.SimpleMove (transform.forward * Speed);

         GetComponent<Animation>().Play(ModRun.name);
 
     } 

     else 
     {
         GetComponent<Animation>().Play(ModIdle.name);


Here is my code. As you can see I'm using a Raycast to do my click to move stuff (I'm following an online tutorial) but the movement doesn't seem to trigger animations in the animator, nor do the animations called in the script play either. I'm not sure where I'm going wrong as I've followed the destructions to the letter (except for using as the guide was using an older version of unity).

Should I be using the raycast to rotate my player and then add.velocity rather than just move to position? would that be better?

Sorry and thank you in advanced for any assistance. Nem.

Comment
Add comment · Show 1
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 Creative Inventory · Dec 23, 2015 at 09:10 PM 0
Share

Could you tell me what online tutorial did you use?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by unity_pq4f9m5BG-5Inw · Feb 24, 2020 at 06:35 AM

Hy @nemervore you can follow this video to make click to move, it is simple, short and clean. https://www.youtube.com/embed/DPBdqbP8MFI

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

52 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

Related Questions

Interchangeable animations in a script? 0 Answers

Looking to activate animation when clicking a button 0 Answers

Calculating Raycasting on terrain? Why cant it hit the surface instead of under or 5 feet above the terrain? 0 Answers

Walking animation on a click to move script 1 Answer

Move GameObject to click position within a sprite 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