From ETC Public Wiki
Jump to: navigation, search

Movement using Transform.Translate:

Movement using transform:

 transform.transform.x += 1;
 transform.rotation.y += .1;  // 0.0 - 1.0 

Rotation via angles - relative


transform.eulerAngles.x, .y, .z translates the angle into degrees

so you can try this too:

 transform.eulerAngles.y += -turnSpeed;

Note: I have had problems with lockup when transform.eulerAngles.y hits 90 and 270. No problems with transform.eulerAngles.x

Moving along the forward vector

 transform.position += transform.forward * Input.GetAxis("Vertical") * speedMoveRate;

You can also add:

 * Time.deltaTime

after speedMoveRate to allow the game to conform to the current frame rate that the pc is running at.

Keeping movement with a range of space

 var minH = -10;
 var maxH = 10;
 var minV = -5;
 var maxV = 13;
 function Move() {
   var horizontal = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
   var vertical = Input.GetAis("Vertical") * speed * Time.deltaTime; 
   var pos : Vector3 = transform.position;
   pos.x = Mathf.Clamp(pos.x + horizontal, minH, maxH);
   pos.y = Mathf.Clamp(pos.y + vertical, minV, maxV);
   transform.position = pos; 

Using Lerps


You have your line of code but for some reasons nothing is happening or it all happens at once. Lerp is a interpolation between two points start and to by a ratio t. So t <=0 the result is the start position and t >=1 then the result is the to position.

 transform.position = Vector3.Lerp(start, to, t);

If you want to move between two points exactly in a set amount of time, then you need to record the starting position and increment t (usually by a factor of Time.deltaTime/NumberOfSecondsToComplete) and the object will reach the destination when t reaches 1.

Like this:

 Vector3 _start;
 Vector3 _target;
 float _t;
 void Update()
    transform.position = Vector3.Lerp(_start, _target, _t);
    _t += Time.deltaTime/2; //Take 2 seconds

 public void SetTargetPosition(Vector3 newTargetPosition)
   _start = transform.position;
   _target = newTargetPosition;
   _t = 0;

Otherwise you might want a smoothed effect where you just move from where you currently are towards the target - in that case you use it differently and keep passing the current position as the first Lerp parameter.

In human words, it means you take the start(perhaps transform.position) and you look at to (the target.position). Then you move the object by the amount of t. If you give t= 1 it moves suddenly from start to to. If you give 0.5 it will move 1/2 of the current distance between the two points each frame.

 function Update(){
   transform.position = Vector3.Lerp(transform.position, target.position, Time.deltaTime);