Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by Thatlegoguy · Jan 22, 2021 at 10:21 AM · movementcharactercontrollerdistance

Gameobject stops moving and weird print statement response

I made a gameobject - called a Villager - which travels on this path made of nodes, which are empty gameobjects that coincide with the first mentioned. The Villager has a character controller, which I use to move it. alt text

below is part of the code responsible for moving it:

 private void MoveVillagerAlongPath()
     {
         SetDistToNextNode();
 
         if (distToNextNode > prevDist)
         {
             ChangeDirection();
             SetDistToNextNode();
             transform.localPosition = SetXZPosition(path[currentNode].localPosition, transform.localPosition.y);
             transform.rotation = Quaternion.LookRotation(dirToNextNode, Vector3.up);
         }
 
         // test //
 
 
 
         // end test //
         
         print("new direction: " + dirToNextNode +
                   ", current node: " + currentNode +
                   "\ncurrent move state: " + curMoveState +
                   ", distance to next node: " + distToNextNode
                   );
 
         prevDist = distToNextNode;
         print(c.Move(dirToNextNode * Time.deltaTime * moveSpeed));
     }

when I print the call to c.Move(...), it doesn't print anything to the console UNTIL the Villager is travelling in the reverse direction (it's supposed to do that), and is a distance of 1.07999... units from the first node, when the Villager suddenly stops moving. The print statement in the console says "Sides". What is going on here? I'll have the complete c# script below:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class VillagerAI : MonoBehaviour
 {
     enum MoveState
     {
         forward,
         backward
     }
 
     enum MovePattern
     {
         backAndForth,
         curcuit
     }
 
     [SerializeField] Transform player;
 
     [SerializeField] Transform[] path;
 
     [SerializeField] MovePattern movePattern1 = MovePattern.backAndForth;
 
     CharacterController c;
 
     [SerializeField] float moveSpeed = 1f;
 
     [Tooltip("The angle to check against to see if player is in the field of view")]
     [SerializeField] float testAngle = 90f;
 
     [Tooltip("The distance to check against to see if the player is in range")]
     [SerializeField] float testDist = 8f;
 
     // The 'y' rotation of the player from the enemy
     float rotationToPlayer = 0f;
 
     // The distance from the enemy
     float distFromVillager = 0f;
 
     int currentNode = 0;
 
     Vector3 dirToNextNode;
 
     float distToNextNode;
 
     float prevDist = 0f;
 
     MoveState curMoveState;
 
     // Start is called before the first frame update
     void Start()
     {
         SetDistToNextNode();
         prevDist = distToNextNode;
         curMoveState = MoveState.forward;
         c = GetComponent<CharacterController>();
         transform.position = SetXZPosition(path[0].position, transform.position.y);
         dirToNextNode = (path[currentNode + 1].localPosition - path[currentNode].localPosition).normalized;
         transform.rotation = Quaternion.LookRotation(dirToNextNode, Vector3.up);
     }
 
     // Update is called once per frame
     void Update()
     {
 
         rotationToPlayer = Quaternion.LookRotation(player.position - transform.position, Vector3.up).eulerAngles.y - transform.rotation.eulerAngles.y;
         distFromVillager = (player.position - transform.position).magnitude;
 
         if ((rotationToPlayer < testAngle && rotationToPlayer > -testAngle) && distFromVillager < testDist)
         {
             // when the villager spots the player
 
         }
 
         MoveVillagerAlongPath();
         
     }
 
     private void OnDrawGizmos()
     {
         Gizmos.color = Color.yellow;
         for(int i = 0; i < path.Length - 1; i++)
         {
             Gizmos.DrawLine(path[i].position, path[i+1].position);
         }
     }
 
     // sets the 'x' and 'z' position of this transform. the 'y' is the 'y' position of the Vector3 being set.
     private Vector3 SetXZPosition(Vector3 pos, float y)
     {
         return new Vector3(pos.x, y, pos.z);
     }
 
     // distance from 'transform.localPosition' to 'toPos'
     private Vector3 GetXZDistance(Vector3 toPos)
     {
         return new Vector3(toPos.x - transform.localPosition.x, 0, toPos.z - transform.localPosition.z);
     }
 
     private void MoveVillagerAlongPath()
     {
         SetDistToNextNode();
 
         if (distToNextNode > prevDist)
         {
             ChangeDirection();
             SetDistToNextNode();
             transform.localPosition = SetXZPosition(path[currentNode].localPosition, transform.localPosition.y);
             transform.rotation = Quaternion.LookRotation(dirToNextNode, Vector3.up);
         }
 
         // test //
 
 
 
         // end test //
         
         print("new direction: " + dirToNextNode +
                   ", current node: " + currentNode +
                   "\ncurrent move state: " + curMoveState +
                   ", distance to next node: " + distToNextNode
                   );
 
         prevDist = distToNextNode;
         print(c.Move(dirToNextNode * Time.deltaTime * moveSpeed));
     }
 
     private void ChangeDirection()
     {
         if (curMoveState == MoveState.forward)
         {
             MakeDirectionForward();
         }
         else if (curMoveState == MoveState.backward)
         {
             MakeDirectionBackward();
         }
     }
 
     private void MakeDirectionForward()
     {
         currentNode++;
         if (currentNode < path.Length - 1)
         {
             dirToNextNode = (path[currentNode + 1].localPosition - path[currentNode].localPosition).normalized;
         }
         else
         {
             curMoveState = MoveState.backward;
             dirToNextNode = (path[currentNode - 1].localPosition - path[currentNode].localPosition).normalized;
         }
     }
 
     private void MakeDirectionBackward()
     {
         currentNode--;
         if (currentNode > 0)
         {
             dirToNextNode = (path[currentNode - 1].localPosition - path[currentNode].localPosition).normalized;
         }
         else
         {
             curMoveState = MoveState.forward;
             dirToNextNode = (path[currentNode + 1].localPosition - path[currentNode].localPosition).normalized;
         }
     }
 
     private void SetDistToNextNode()
     {
         if (curMoveState == MoveState.forward)
         {
             if (currentNode < path.Length - 1)
             {
                 distToNextNode = GetXZDistance(path[currentNode + 1].localPosition).magnitude;
             }
         }
         else if (curMoveState == MoveState.backward)
         {
             if (currentNode > 0)
             {
                 distToNextNode = GetXZDistance(path[currentNode - 1].localPosition).magnitude;
             }
         }
     }
 }

villager-path.png (286.7 kB)
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

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.

Follow this Question

Answers Answers and Comments

176 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

Related Questions

Script Stopped Working - Walking on Walls 1 Answer

My player keeps launching into the sky 1 Answer

Changing walking speed in CharacterController.Move 1 Answer

How do I make my character controller jump when I use this code? 0 Answers

change move controls relative to player 1 Answer


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