How can ı go slowly to position whick i clicked ?

When ı left click, it looks like i am teleporting. I want to click and go slowly to mouse click position. How can ı do that ? Thanks

Hata indir Hata indir

Vector3 newPosition;
      void Start () {
          newPosition = transform.position;
      }
      void Update()
      {
          if (Input.GetMouseButtonDown(0))
          {
              RaycastHit hit;
              Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
              if (Physics.Raycast(ray, out hit))
              {
                  newPosition = hit.point;
                  transform.position = newPosition;
              }
          }
      }


  }

Use Vector3.MoveTowards using your newPosition as the target: Unity - Scripting API: Vector3.MoveTowards

1 Like

can also add coroutine loop, to easily move within certain time duration:
http://answers.unity.com/answers/1728384/view.html

or, if want to add easing, or use own curves see thread:

H

This is final version of my code but still it is not working like i wanted it. When ı click it goes like teleporting or fast movement. When ı click to a position which ı wanted to go, ı want do that slowly without constantly. how can ı apply what you have said to my code ? Thanks

public float speed = 15.0f;
    // Start is called before the first frame update
    Vector3 newPosition;
      void Start () {


       
      }
      void Update()
      {
         float step = speed * Time.deltaTime;

          if (Input.GetMouseButtonDown(0))
          {
              RaycastHit hit;
              Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
              if (Physics.Raycast(ray, out hit))
              {
  newPosition = hit.point;

           transform.position = Vector3.MoveTowards(transform.position, newPosition, step);


              }
          }

Move line 21 downward outside the if ( ) block, so it runs every Update.

Edit: I would suggest when you’re testing things, start out with rather slow values for speed. Right now you have speed = 15f which (depending on the scale of your world) would represent 15 meters per second, which is faster than Usain Bolt sprinting.

1 Like

https://www.youtube.com/watch?v=Nqq7H33HH8E

What i am making is a spiderman swinging game. I want a feature like between 31 and 33 seconds of this video(another example 1:10 1:11). Going to mouse click position with time and without lagging or teleporting. Constant movement. What ı have to change for that ? Thanks for help

Exactly what Grozz says here:

Looking at this game there looks like there’s many different movement modes they’re switching between over time. It’s either freefall, wallrun, web sling, web pull, and probably a bunch of other subtle variations of the above. ALL of them just change the velocities applied to the player, and of course trigger appropriate animations.

I really tried but it is still lagging and there is no constant movemet. Do you have any alternative solution ? Thanks.