Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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 /
avatar image
0
Question by radev2000 · Dec 26, 2019 at 10:26 PM · movementnavmeshagentplayer movementcharacter controllercharacter movement

3D RPG - Player movement - He automatically finds the fastest way and goes around everything. How to limit his movement?

Hello guys, I'm working on a rpg game like Diablo. I wanted to make my character move when I Right click on the terrain and I did it by making two scripts - one for the movement itself and one for the controllers (left / right clicking) BUT he finds the fastest way to go to the place. For example if I have made something like barriers between two zones. My character is in Zone 1. When I click on Zone 2 he automatically finds the fastest way and goes around the barriers till he is in the place I clicked in Zone 2. I want him to move when I click somewhere but I don't want him to explore the map himself by finding the fastest way.

I will put my movement script here:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 /* This component moves our player using a NavMeshAgent. */
 
 [RequireComponent(typeof(NavMeshAgent))]
 public class PlayerMotor : MonoBehaviour
 {
     Transform target;       // Target to follow
     NavMeshAgent agent;     // Reference to our agent
 
     // Get references
     void Start()
     {
         agent = GetComponent<NavMeshAgent>();
     }
     void Update()
     {
         if(target != null)
         {
             agent.SetDestination(target.position);
             FaceTarget();
         }
     }
 
     public void MoveToPoint(Vector3 point)
     {
         agent.SetDestination(point);
     }
 
     public void FollowTarget(Interactable newTarget)
     {
         agent.stoppingDistance = newTarget.radius * .8f;
         agent.updateRotation = false;
         target = newTarget.interactionTransform;   
     }
     public void StopFollowingTarget()
     {
         agent.stoppingDistance = 0f;
         agent.updateRotation = true;
         target = null;      
     }
 
   void FaceTarget()
     {
         Vector3 direction = (target.position - transform.position).normalized;
         Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0f, direction.z));
         transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
     }
 }

and here is the one with the controllers:

 using UnityEngine.EventSystems;
 using UnityEngine;
 
 /* Controls the player. Here we choose our "focus" and where to move. */
 
 [RequireComponent(typeof(PlayerMotor))]
 public class PlayerController : MonoBehaviour
 {
     public Interactable focus;
 
     public LayerMask movementMask;  // Filter out everything not walkable
 
     Camera cam;         // Reference to our camera
     PlayerMotor motor;  // Reference to our motor
 
     // Get references
     void Start()
     {
         cam = Camera.main;
         motor = GetComponent<PlayerMotor>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (EventSystem.current.IsPointerOverGameObject())
             return;
 
         // If we press left mouse
         if (Input.GetMouseButtonDown(0))
         {
             // We create a ray
             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
 
             // If the ray hits
             if (Physics.Raycast(ray, out hit, 100, movementMask))
             {
                 motor.MoveToPoint(hit.point);   // Move to where we hit
                 RemoveFocus();
             }
         }
 
         // If we press right mouse
         if (Input.GetMouseButtonDown(1))
         {
             // We create a ray
             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
 
             // If the ray hits
             if (Physics.Raycast(ray, out hit, 100))
             {
                 Interactable interactable = hit.collider.GetComponent<Interactable>();
                 if(interactable != null)
                 {
                     SetFocus(interactable);
                 }
             }
         }
     }
     void SetFocus(Interactable newFocus)
     {
         if (newFocus != focus)
         {
             if (focus != null)
                 focus.OnDefocused();
 
             focus = newFocus;
             motor.FollowTarget(newFocus);
         }
         newFocus.OnFocused(transform);
         
     }
     void RemoveFocus()
     {
         if (focus != null)
             focus.OnDefocused();
 
         focus = null;
         motor.StopFollowingTarget();
     }
 }
 



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
1

Answer by GanemVisk · Dec 26, 2019 at 11:45 PM

I believe you can use NavMesh obstacles to carve a moat around the screen and then check if there is a path to the destination before setting it.

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

177 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

Related Questions

Character Motor - Make it move in Global Space 0 Answers

Player acts weirdly when I release movement input 0 Answers

I'm not too sure what's wrong with my if statement... 0 Answers

Character Controller Move in X and Z axis via camera 1 Answer

Character controler Script 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