Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help with move script

Discussion in 'Scripting' started by takito, Nov 18, 2015.

  1. takito

    takito

    Joined:
    Apr 15, 2013
    Posts:
    11
    Hello, I'm new at C# but understand basics and maybe even intermediate, my questions is the following:
    Im working on a 2d,
    Why is it that when I use Vector3 on the blue Vector3 area of the script, my object eases into the mouse
    position??

    On the other hand if I change to Vector2 like the summary says it will no longer ease??

    Its not causing any trouble but I always like to know why things happen.
    Could it be the z property of the Vector3 that is making the difference??
    Tested and let me know.

    thanks


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MovementTowardsMouseClick : MonoBehaviour {
    6.  
    7.     public float speed; //For setting the speed
    8.     private Vector3 mousePosition; //For getting the mouse position
    9.     private Vector3 thisObject; //For getting this objects position
    10.  
    11.     void Update()
    12.     {
    13.         //store the current position
    14.         thisObject = this.transform.position;
    15.  
    16.         //if the right mouse is pressed
    17.         if (Input.GetMouseButton(0))
    18.         {
    19.             //store the mouse position
    20.             mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    21.          
    22.         }
    23.  
    24.         //
    25.         // Summary:
    26.         // //
    27.         //  Note that if you change the Vector3 type to Vector2 and comment out
    28.         //  "thisObject.z = this.transform.position.z" line the object will no longer
    29.         // ease its way in to the mouse position
    30.         // //
    31.  
    32.         thisObject = Vector3.MoveTowards(thisObject, mousePosition, speed * Time.deltaTime);
    33.         hisObject.z = this.transform.position.z;
    34.         this.transform.position = thisObject;
    35.      
    36.  
    37.     }
    38. }
     
    Last edited: Nov 18, 2015
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Use [code ]code tags[/code] to make your code more readable; help us help you!

    Your guess is probably correct.
    Try outputting the Vector2's that you're using, and the Vector3's you're using, and compare the numbers:
    Code (csharp):
    1. Debug.Log("Moving from "+thisObject+" to "+mousePosition);
    If the Z property is the difference, you'll see a big difference between the last number of each of those outputs.
     
  3. takito

    takito

    Joined:
    Apr 15, 2013
    Posts:
    11
    Thanks for your quick response I will try that.