• 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
Question by CrazyCGChick85 · Nov 08, 2018 at 07:03 PM · animationrotationcollisioncollectible-game-objects

How do I make my character face the right direction?

HI Guys,

Have been working on a click to move type game. Currently I have a character that walks around a basic scene collecting objects that I click on. During animation the character walks and faces the right direction, however when I click on an object to collect the character will walk up to the object and then suddenly face the Z direction. I have looked at the LookAt function but it didn't seem to work.

Some help will be great

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI; //Needs to be added to be used with the events system
 using UnityEngine.EventSystems; //Needs to be added when an event system ia required
 
 [RequireComponent(typeof(Animator))]
 
 public class PlayerMovement : MonoBehaviour
 {
     public Animator animator;
     public NavMeshAgent agent;
     public float inputHoldDelay = 0.5f;
     public float turnSpeedThreashold = 0.5f;
     public float speedDampTime = 0.1f;
     public float slowingSpeed = 0.175f;
     public float turnSmoothing = 15f;
     public bool isStopped;
     public Transform target;
 
     private WaitForSeconds inputHoldWait;
     private Vector3 destinationPosition;
     private Interactable currentInteractable;
     private bool handleInput = true;
     
 
     private const float stopDistanceProportion = 0.1f;
     private const float navMeshSampleDistance = 4f;
 
     private readonly int hashSpeedPara = Animator.StringToHash("Speed");
     private readonly int hashLocomotionTag = Animator.StringToHash("Locomotion");
 
     private void Start()
     {
         agent.updateRotation = false; //Don't update mesh rtoation
 
         inputHoldWait = new WaitForSeconds(inputHoldDelay);
 
         destinationPosition = transform.position;
 
     }
 
     /*private void OnAnimatorMove()
     {
         //root motion contoller for imported player
         Animator animator = GetComponent<Animator>();
 
         if (animator)
         {
             Vector3 newPosition = destinationPosition; //Does nothing but if removed it breaks everything
             newPosition = animator.deltaPosition / Time.deltaTime;
         }
 
     }*/
 
     private void Update()
     {
       //  RaycastHit hit;
          
         if (agent.pathPending)
         {
             return;
         }
 
         float speed = agent.desiredVelocity.magnitude;
 
         if (agent.remainingDistance <= agent.stoppingDistance * stopDistanceProportion)
         {
             Stopping(out speed);
         }
 
         else if (agent.remainingDistance <= agent.stoppingDistance)
         {
             Slowing(out speed, agent.remainingDistance);
         }
 
         else if (speed > turnSpeedThreashold)
         {
             Moving();
         }
        animator.SetFloat(hashSpeedPara, speed, speedDampTime, Time.deltaTime);
 
 
     }
 
     private void Stopping(out float speed)
     {
         //If agent has stopped, it needs to stop and stay at new position
         agent.isStopped = true; // replaces agent.Stop();
 
         transform.position = destinationPosition;
         speed = 0f;
 
         if (currentInteractable)
         {
             transform.rotation = currentInteractable.interactionLocation.rotation;
             currentInteractable.Interact();
             currentInteractable = null;
             StartCoroutine(WaitForInteraction());
         }
 
     }
 
     private void Slowing(out float speed, float distanceToDestination)
     {
         agent.isStopped = true;
         transform.position = Vector3.MoveTowards(transform.position, destinationPosition, slowingSpeed * Time.deltaTime);
 
         float proportionalDistance = 1f - distanceToDestination / agent.stoppingDistance;
         speed = Mathf.Lerp(slowingSpeed, 0f, proportionalDistance);
     }
 
     private void Moving()
     {
         Quaternion targetRotation = Quaternion.LookRotation(agent.desiredVelocity);
         transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, turnSmoothing * Time.deltaTime);
     }
 
     public void OnGroundClick(BaseEventData data)
     {
         PointerEventData pData = (PointerEventData) data;
         NavMeshHit hit;
         if (NavMesh.SamplePosition(pData.pointerCurrentRaycast.worldPosition, out hit, navMeshSampleDistance, NavMesh.AllAreas))
         {
             destinationPosition = hit.position;
         }
 
         else
         {
             destinationPosition = pData.pointerCurrentRaycast.worldPosition;
         }
 
         agent.SetDestination (destinationPosition);
         agent.isStopped = false; // replaces agent.Resume();
     }
 
     public void OnInteractableClick (Interactable interactable)
     {
         Debug.Log("Object collected first");
 
         if (!handleInput)
         {
             return;
         }
         Debug.Log("Object collected second "+interactable.interactionLocation);
         currentInteractable = interactable;
         destinationPosition = currentInteractable.interactionLocation.position;
         //destinationPosition = new Vector3(0,0,10);
         agent.SetDestination(destinationPosition);
        
         agent.isStopped = false;
         //Debug.Log("Object collected third");
     }
 
     private IEnumerator WaitForInteraction ()
     {
         handleInput = false;
 
         yield return inputHoldWait;
 
         while(animator.GetCurrentAnimatorStateInfo(0).tagHash != hashLocomotionTag)
         {
             yield return null;
         }
 
         handleInput = true;
     }
 
 }
 
Comment

People who like this

0 Show 0
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

0 Replies

· Add your reply
  • Sort: 

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

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

Why am I getting these errors for my collision animation? 1 Answer

I want to trigger an animation upon collision with a 2D obstacle in my infinite runner game.Help me. 0 Answers

Enemy leans back/ rotates when colliding with other game objects, any fixes? 1 Answer

Smooth rotation on animation 1 Answer

Inverse kinematic strange rotation 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