Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Move object

Discussion in 'Scripting' started by Maximchuk, Mar 1, 2015.

  1. Maximchuk

    Maximchuk

    Joined:
    Jan 19, 2015
    Posts:
    5
    Good day

    I Want the object is moving with the mouse but with a certain speed
    there is the following code:

    but the object is moving with a speed of mouse.
    Code (CSharp):
    1. ay = new Ray (Camera.main.ScreenToWorldPoint(Input.mousePosition),Vector3.zero);
    2.  
    3.         if(Input.GetMouseButton(0))
    4.         {
    5.             transform.position = new Vector2 (ray.direction.x,0,0);
    6.  
    7.         }        
     
  2. Jmanpie

    Jmanpie

    Joined:
    Oct 7, 2012
    Posts:
    132
    Could you not lerp this would make the object move over a certain time towards the mouse object you an change the speed by using time.delttime and * by a value 1 > = faster 1 < = slower.

    http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

    If you dont want it to go to the exact position of the mouse create an offset.
     
  3. Maximchuk

    Maximchuk

    Joined:
    Jan 19, 2015
    Posts:
    5
    Thank you Jmanpie for the advice, going to try