Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 rywmkg · Nov 29, 2021 at 05:31 PM · vector3navmeshagenttransform.positionnavigationvector3.movetowards

Vector3.MoveTowards and NavAgent weirdness

Hi, this is a repost from this forum thread. Just thinking this is a more appropriate place to post it. I'm having trouble with weird motions coming out of Vector3.MoveTowards and the NavMeshAgent system.

Here's the setup: I have a player gameObject with a NavMeshAgent component; you can click on the terrain to set the agent's destination. And then I have a child gameObject the position of which is some distance between the player's current position and the agent's destination. The idea is that this object will be a focal point for the camera, and it leads ahead the player in the direction it's travelling. (Cinemachine can only do this if it's allowed to rotate, which I don't want). I want this focal object to gradually move around, instead of just snapping around as new destinations are set, so I have it moving with Vector3.MoveTowards. (I have also tried Vector3.Lerp and Vector3.Slerp and they all do the same thing).

I've attached a screen cap which I slowed down to 5fp for clarity. The green line is the player's forward direction, the solid red disc is the NavMeshAgent's destination, and the small blue circle is the focus point I am concerned with.

(The gif is too big to attach here, but you can see it on this forum post).

You can see that it starts out okay, but when the player turns around it goes bananas and gets behind the player, and then it straightens out again. But I don't reference the player's direction anywhere - all this object knows is the player's position and the destination position, so it shouldn't be affected by the player's facing direction at all. So I guess my question is: why is this game object being influenced by the player's transform rotation, if it only knows the transform position of the player and the agent destination?

Here is the code I am using to set the position of the focus point. I call this in Update. (I am clamping the desired point so it doesn't get too far away, because the camera will follow it and the player will then be offscreen).

 private void SetPosition()
 {
     destinationDirection = Vector3.Normalize(agent.destination - player.transform.position);
     destinationDistance = Vector3.Magnitude(agent.destination - player.transform.position);
     desiredFocusPoint = player.transform.position + Vector3.ClampMagnitude(destinationDirection * destinationDistance, clampMagnitude);
     transform.position = Vector3.MoveTowards(transform.position, desiredFocusPoint, moveTowardSpeed * Time.deltaTime);
 }


Thanks for your help!

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

204 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

Related Questions

check if object is reachable on NavMesh 1 Answer

Vector 3 moving to certain place? 2 Answers

Enemy Behaviour And Navigation Being Quite Odd. 0 Answers

PathFinding Lag With NaMeshAgent 0 Answers

NavMesh Agent doesn't work with slope 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