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
1
Question by apixalET · Sep 12, 2015 at 12:58 AM · c#raycastingcharacter controllerclicktomove

Third Person Controller - How to Click to Move?

Hi everyone,

This is driving me crazy, sifting through numerous articles on the best way to accomplish this.

Simply put, I am using the Third Person Controller prefab (Ethan) from the Standard Asset package. Instead of using the arrow keys to move him, I'd like to have him move to where the player clicks.

The code I have so far just barely works, and is not usable, but hopefully it will give you an idea for what I am trying to accomplish. I was able to get it working better than this, but the Ethan Animation Controller broke, and none of his animations walk/run would happen on his way to the final destination, so I tried to tie in Unity's script to accomplish that.

Any guidance would make me rest peacefully tonight! Thanks for your help in advance.

My Script (attached to the Player Object)

 using UnityEngine;
 using System.Collections;
 using UnityStandardAssets.Characters.ThirdPerson;
 
 public class clickToMove : MonoBehaviour 
 {
 
     private ThirdPersonCharacter thirdPersonScript;
 
     private Transform playerTransform;
 
     private bool completed;
 
     void Start()
     {
         //Link Scripts
 
         thirdPersonScript = gameObject.GetComponent<ThirdPersonCharacter>();
 
         //Link Objects
 
         playerTransform = gameObject.GetComponent<Transform> ();
     }
 
     void Update()
     {
         if (Input.GetMouseButtonDown (0))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
 
             if(Physics.Raycast(ray, out hit));
             {
                 Debug.Log ("I hit something!");
                 StartCoroutine(movePlayer(hit.point));
             }
         }
     }
 
     IEnumerator movePlayer (Vector3 finalDestination)
     {
         completed = false;
         
         var i     = 0.0f;
 
         float journeyLength = Vector3.Distance(playerTransform.position, finalDestination);
 
         while (journeyLength > 1.0f)
         {
             journeyLength = Vector3.Distance(playerTransform.position, finalDestination);
             thirdPersonScript.Move (Vector3.Lerp(playerTransform.position, finalDestination, 10F), false, false);
             Debug.Log (journeyLength);
             yield return null;
         }
 
         Debug.Log ("Done!");
 
         completed = true;
 
     }
 }
 

Unity's ThirdPersonCharacter.cs for reference

 public void Move(Vector3 move, bool crouch, bool jump)
         {
 
             // convert the world relative moveInput vector into a local-relative
             // turn amount and forward amount required to head in the desired
             // direction.
             if (move.magnitude > 1f) move.Normalize();
             move = transform.InverseTransformDirection(move);
             CheckGroundStatus();
             move = Vector3.ProjectOnPlane(move, m_GroundNormal);
             m_TurnAmount = Mathf.Atan2(move.x, move.z);
             m_ForwardAmount = move.z;
 
             ApplyExtraTurnRotation();
 
             // control and velocity handling is different when grounded and airborne:
             if (m_IsGrounded)
             {
                 HandleGroundedMovement(crouch, jump);
             }
             else
             {
                 HandleAirborneMovement();
             }
 
             ScaleCapsuleForCrouching(crouch);
             PreventStandingInLowHeadroom();
 
             // send input and other state parameters to the animator
             UpdateAnimator(move);
         }
 

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 EssyTech · Sep 12, 2015 at 07:06 AM

The animations for ethan depend on two vectors. m_TurnAmount and m_ForwardAmount. Make sure those are referenced in the code that runs the animations. or recreate them based on the move vector you are passing through to the UpdateAnimator() function.

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

Character controller and determinism issue 0 Answers

Rope & Character controller ? 0 Answers

Raycast never gets called 1 Answer

Proper use of Character Controller 2 Answers

Raycast doesn't stay in one place 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