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 cagdaskml · Feb 20 at 05:45 PM · mouseclickmove an object

How do i move the game object towards the mouse clicked point smoothly?

Video of gameplay

Hello guys, I am new in unity development(3 months) and I try to improve myself by developing games. I want to move my gameObject to mouse click position. You can see my problem in video above. The player move to clicked direction but it's not complete the distance. When I click the position the player move on the right direction but if the mouse click position far, it can move only half of distance or maybe less.When its on the same x axis it move right but in cross movement it can't reach the position with one click.

Thats the player's script. I want to move the player to clicked point smoothly so I'm waiting for your help, thanks in advance. İf I increase the moveSpeed on the code below it can reach the point but ıt's too fast so ı don't want to do this.

 public class UnitRTS : MonoBehaviour
 {
     public bool isSelected;
     public GameObject select;
     public float moveSpeed = 50f;
     private Rigidbody rb;
     Vector3 newPosition;
 
     private Animator anim;
 
     Vector3 targetPosition;
     Vector3 pos;
 
     private Enemy enemy;
 
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody>();
 
         isSelected = false;
         newPosition = transform.position;
 
         anim = GetComponent<Animator>();
 
         targetPosition = transform.position;
         enemy = GameObject.FindWithTag("Enemy").GetComponent<Enemy>();
     }
 
     private void FixedUpdate()
     {
          
     }
 
     // Update is called once per frame
     void Update()
     {
         Selected();
         LookTowardMouse();
         Move();
         
         
     }
 
     void Selected()
     {
             if (isSelected)
             {
                 select.gameObject.SetActive(true);
             }
             else
             {
                 select.gameObject.SetActive(false);
             }
      }
 
     void LookTowardMouse()
     {
             if (isSelected)
             {
                 //Get the Screen positions of the object
                 Vector2 positionOnScreen = Camera.main.WorldToViewportPoint(transform.position);
 
                 //Get the Screen position of the mouse
                 Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
 
                 //Get the angle between the points
                 Vector3 a = positionOnScreen;
                 Vector3 b = mouseOnScreen;
                 float angle = Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg; //AngleBetweenTwoPoints(positionOnScreen, mouseOnScreen);
 
                 //Ta Daaa
                 transform.rotation = Quaternion.Euler(new Vector3(0f, -angle - 65f, 0f));
             }
 
     }
 
     public void Move()
     {
         if (isSelected && Input.GetMouseButtonUp(1))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
 
             if (Physics.Raycast(ray, out hit))
             {
                 targetPosition = hit.point;
                 Vector3 dir = (targetPosition - transform.position).normalized;
                 transform.position += dir * Time.deltaTime * moveSpeed;
 
                 Vector3 temp = transform.position;
                 temp.y = 0.4f;
                 transform.position = temp;
 
                 //anim.SetBool("isWalking", true);
                 anim.SetBool("isCharge", true);
             }
         }
         else
         {
             //anim.SetBool("isWalking", false);
             anim.SetBool("isCharge", false);
         }
     }
 
     private void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Enemy")
         {
             anim.SetBool("isAttack", true);
             enemy.isAngry = true;
         }
     }
 
     private void OnTriggerExit(Collider other)
     {
         if (other.tag == "Enemy")
         {
             anim.SetBool("isAttack", false);
         }
     }
 
 }

Comment
Add comment · Show 1
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 cagdaskml · Feb 23 at 09:28 AM 0
Share

I would like to point out that there may be people who haven't seen it. Still waiting for your help.

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

175 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

Related Questions

impact mouseclick 0 Answers

Camera Script remove a part. 0 Answers

How do I use an event trigger on an instantiated prefab? 1 Answer

How to change the color of the each phase of the cube separately ? 0 Answers

Could someone help me figure out which part of this code relates to mouse button click simulation please? 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