• 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 Cuvava11 · Jul 10, 2020 at 06:35 PM · movementmovement scriptmovetowardsrotatetowardsmovements

MoveTowards is moveing my object to random position when i click it is already on a way

Hello, i want to rotate and after it move my 2D object to clicked position. I used RotateTowards for rotate and MoveTowards for move my object. But sometimes when i click to any position when it is already on a way to position, it is going to random position, not to that first clicked position or second one. I can't understand why sometimes doing this. Also i am open to another ideas about make better my codes because this is my first Unity script. I am not experienced about that.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Movement : MonoBehaviour
 {
     private float movementSpeed = 2;
     private float rotationSpeed = 300;
 
     private bool IsRotating = false;
     private bool rotated = false;
     private bool IsMoving = false;
 
     private Vector3 targetPosition;
     private Quaternion rotation;
 
     void Update()
     {
         if(Input.GetMouseButtonDown(0))
         {
             Debug.Log("Tiklandi.");
             IsRotating = false;
             rotated = false;
             IsMoving = false;
             SetAngle();
         }
 
         if(IsRotating == true && IsMoving == false && rotated == false)
         {
             Rotation(); 
         }
 
         if (rotated == true && IsRotating == false && IsMoving == false)
         {
             SetTargetPosition();
         }
 
         if (IsMoving == true && rotated == true && IsRotating == false)
         {
             Move();
         }
     }
 
     void SetAngle()
     {
         Debug.Log("SetAngle calisti.");
         Vector2 dir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
         float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
         rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);
 
         IsRotating = true;
     }
 
     void Rotation()
     {
         Debug.Log("Rotation calisti.");
         transform.rotation = Quaternion.RotateTowards(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
 
         if (transform.rotation == rotation)
         {
             IsRotating = false;
             rotated = true;
         }
     }
 
     void SetTargetPosition()
     {
         Debug.Log("SetTargetPosition calisti.");
         targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         targetPosition.z = transform.position.z;
 
         Debug.Log(targetPosition);
         IsMoving = true;
     }
 
     void Move()
     {
         Debug.Log("Move calisti.");
         transform.position = Vector3.MoveTowards(transform.position, targetPosition, movementSpeed * Time.deltaTime);
         if (transform.position == targetPosition)
         {
             rotated = false;
             IsMoving = false;
         }
     }
 }
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 Happeloy · Jul 11, 2020 at 12:08 AM

You're setting the target position after the rotation is done. And when you set it, you set it to the current mouse position, which has most likely moved since you actually pressed your mouse. I guess that is your problem. Instead, you should store the mouse position in your first if-statement, when you check for the mouse down, and use that position when you set the new goal position.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

198 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

Related Questions

Boundaries for Camera Movement 1 Answer

Interrupt a MoveTowards when triggering 2 Answers

Script click to move not working, MoveTowards always move forward 3 Answers

How to make fish movement with keyboard arrows look natural? 1 Answer

Move Character to touched Position 2D (Without RigidBody and Animated Movement) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges