Smooth movement

I’m trying to animate an object going across the screen smoothly in js. So far I have not been able to do it without lag. Even after I deleted all of my GUI.

Are there any rules or guidelines I should be using?

Hello

try this.

in your update function,

speed = Mathf.SmoothDamp(speed,speedToReach, yVelocity, timeToChangeSpeed);	
MoveTowards();

in the MoveTowards function :

function MoveTowards () {
	if(myWayPoint)
	{
		var direction = myWayPoint.transform.position - myself.position;
		direction.y = 0;
		if (direction.magnitude < 0.5) {
			return;
		}

		myself.rotation = Quaternion.Slerp (myself.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
		myself.eulerAngles = Vector3(0, myself.eulerAngles.y, 0);

		var forward = myself.TransformDirection(Vector3.forward);
	
		direction = forward * speed;

		var myCharacterController : CharacterController = GetComponent (CharacterController);
		myCharacterController.SimpleMove(direction);	
	}
}

where :

  • mySelf is a cache to the GameObject you have attached that script to
  • myWayPoint is the transform you want to reach
  • speed is the current speed of your character
  • speedToReach is the spead you want to reach. You character will accelarate until it reached this speed.
  • you need yo attach a character controller to the gameobject
  • timeToChangeSpeed is the speed to turn
  • yVelocity is set to 0 if you want to move X and Z only.

Your character will turn and move towards the target…

give this a try.

hervé

I should have mentioned I’m working on a 2d side scroller where the moving obstacles travel across the screen at increasing rates of speed.
I’m currently trying this ok results, but not great. They still lag and skip at
iTween.MoveFrom(enemy0,{“x”: 540,“y”: 0,“z”: 0, “time”:crossScreenSpeed, “easetype”:“linear”, “oncomplete”:“ResetE0”, “oncompletetarget”:Main_Camera} );

I’ll try you post and see how it performs, thanks! :slight_smile:

Unfortunately, I’m unable to get your script to move my character forward. It turns towards the waypoint, then starts to slowly drop in the Y, never moving forwards to the target. :confused: