Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Chocolade · Oct 23, 2016 at 11:08 PM · c#scripting problemscript.

How can i avoid mouse double click on second time to make my player idle ?

What i'm doing is:

  1. When the game is starting my player is Idle. And i can move the mouse cursour around and the player will rotate and face the mouse position.

  2. When i click one click somewhere on the ground with the mouse the player will start walking to this direction.

  3. When the player get to the mouse clicked position he should Idle again like when i was running the game first time. But this is the problem:

When the player is getting to the clicked mouse position he start rotating like crazy. Only if i click double mouse click he will idle and stop rotating. I'm not 100% sure if i make mouse double left button click make it idle again not sure what is going on.

But what i want is one click make the player start walking to this mouse position when he get to this position Idle the player and let the user move the mouse around again so the player will rotate according to the mouse cursour position.

This is my script: With the _animator i make the player to Walk(and move) or to Idle.

 using UnityEngine;
 using System.Collections;
 
 public class MoveObjects : MonoBehaviour 
 {
     private Animator _animator;
 
     // Use this for initialization
     void Start()
     {
         isWalking = true;
         _animator = GetComponent<Animator>();
         _animator.CrossFade("Idle", 0);
     }
 
     void Update()
     {
         MovePlayerWithMouse();
     }
 
     private void MovePlayerWithMouse()
     {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, out hit) && hit.collider.name != "ThirdPersonController")
         {
             transform.LookAt(hit.point);
         }
         else
         {
             transform.LookAt(ray.GetPoint(100));  //the number here is compltely arbitrary
         }
         if (Input.GetMouseButtonDown(0))
         {
             if ((transform.position - hit.point).magnitude < 1.0f)
             {
                 _animator.CrossFade("Idle", 0);
             }
             else
             {
                 _animator.CrossFade("Walk", 0);
             }
         }
     }
 }



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
0

Answer by b1gry4n · Oct 24, 2016 at 04:15 AM

You need a distance check for the target to say if the object reached it or not. It will constantly try to "lookat" that position right now, which is causing the crazy rotation. Your hit.point is only updated if a valid surface for the mouse is hovered over. If the mouse isnt, you are feeding in a ray as the target. The target he is looking at is the ray position, but hes trying to move to the hit.point (which is no longer a valid position since the mouse moved) Store the target the object is walking towards, check if the distance falls below a threshold, then transition back to idle state if the target is reached.

As youve noticed, things are going to only get more complex from here. I suggest you implement a state machine that way you can easily switch between these states (idle, move, etc). I suggest you dont combine the mouse hit check with the movement of the object. Distinguish them as 2 different methods. The mouse target info is fed into the state machine.

  public float targetReachedDistance = 0.1f;    
 
       if (target != null)
         { //move the player to the target
             if (Vector3.Distance(transform.position, target.position) <= targetReachedDistance)
             {
                 //we reached the destination
                 target = null;
             }
         }
         else
         {
             //idle
         }

Its ok to have more than 1 script. Dont try to do too many different things inside of 1 script. At this point, personally, I would have split up your script into 3 different scripts.

  • PlayerController, which controls the movement and states of the player

  • MouseTargetController, which feeds the mouse target info into the player controller

  • AnimationController, which reads the player controllers state and updates the animation based off that

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

233 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

Related Questions

Why i'm getting UnassignedReferenceException when running the game ? 1 Answer

How can i make the spaceship to land/take off with physics vertical ? 2 Answers

To pass a variable between scenes should I use scriptableobject or static ? 1 Answer

How can i lock the mouse cursor in the middle of the screen ? 1 Answer

Why InputField don't have the property text ? 2 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