Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 MagiBen · Mar 31, 2020 at 07:41 AM · shootingmovement scriptisometricmovements

How to stop player movement while shooting?

Hello!

I have made a simple ray character control script, where when I use the left mouse button, the character follows the mouse point and wants to reach the destination.

Here's the simple code:

 public class PlayerMovement : MonoBehaviour
 {
     NavMeshAgent agent;
 
     // Start is called before the first frame update
     void Start()
     {
         agent = GetComponent<NavMeshAgent> ();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetMouseButton(0))
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
             if (Physics.Raycast(ray, out hit, Mathf.Infinity))
             {
                 agent.SetDestination(hit.point);
             }
         }
     }
 }  


Now I want to enhance my character, so that when I use my right mouse button the character no longer want's to reach the previous destination, locks in one place (except rotates to the mouse pointer), while casting a spell/shoot/hit etc.

Right now I can shoot a fireball, but the character continuously rotates to my mouse and if I shoot, the character won't stop, until it reaches the previous destination.

I'm looking for a general answer, what should I check or where should I begin, what kind of scripts do I need.

Thank you!

Ps.: It's an isometric 3d game, if it matters.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by vilemuse · Nov 09, 2020 at 03:41 AM

Did you ever find a solution? I'm having the exact same issue with my shooting as well.

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

Answer by Rosinal · Nov 09, 2020 at 12:56 PM

I always use aaa tap d ddd tap a, but i never use www s/sss w. i kind of got used to a style that i never walk straight forward if im encountering an enemy, if i peek i always only do a/d + my mouse movement. just because it feels so weird to to the www s/sss w somehow https://www.krogereschedule.us/

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

Answer by merctraider · Nov 09, 2020 at 08:15 AM

Having a variable to keep track of your player states might help. When you want it to stop moving, just set agent.isStopped = true;

I've put together a basic skeleton that you could reference in this, just bundle the various things that the player should do into separate methods that would change depending on the player's actions

     enum PlayerState {Moving, Stopped, Shooting}
     PlayerState currentState = PlayerState.Stopped; 
 
     bool isShooting = false; //Variable to prevent spam 
 
     void Update()
     {
         if(Input.GetMouseButton(0))
         {
             Move();
         }
 
 
         //If you hold right click, you aim 
         if(Input.GetMouseButton(1))
         {
             Aim();
         }
 
         //I'm assuming your system is hold down and release, so upon releasing you can do the shooting here 
         if(Input.GetMouseButtonUp(1))
         {
             Shoot();
 
         }
 
         if(currentState == PlayerState.Stopped || currentState == PlayerState.Shooting)
         {
             agent.isStopped = true; 
         }
              
     }
 
     void Shoot(){
         
         //Since this is called in update you should make sure it's called one at a time
         if(isShooting)
         {
             return;
         }
 
         isShooting = true;
         StartCoroutine("ShootCo");
     }
 
     //How I would do it is a coroutine so there is a delay before your character moves again
     IEnumerator ShootCo()
     {
         //Do the shooting 
         currentState = PlayerState.Shooting; 
         yield return new WaitForSeconds(1f);
         currentState = PlayerState.Stopped; //You're back to stopped state and awaiting further action
     }
 
 
 
 
     void Aim(){
         currentState = PlayerState.Stopped; 
         //Do the aiming stuff here
     }
 
     void Move()
     {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out hit, Mathf.Infinity))
         {
 
             agent.isStopped = false; //If the player was stopped before make it continue
             agent.SetDestination(hit.point);
             currentState = PlayerState.Moving; //Mark player as moving  
             
         }
  }
              
 




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

134 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

Related Questions

[2d] Player sometimes doesn't jump up while moving and jumping at the same time 1 Answer

Isometric movement 3 Answers

How can i make it so that the object doesnt instantly go to top speed. Here is my code 1 Answer

Boundaries for Camera Movement 1 Answer

How to Make Velocity.y based on AddForce 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