Transform are used for storing and manipulating objects Position , Rotation and Scale or Size .
In this blog we will use transform to move an object.Moving an object by changing its transform does not require a Rigidbody, that way its hard to get collision events of the object .
Below code can be used to move a object using transform :
using UnityEngine;
using System.Collections;
public class BlockMovement : MonoBehaviour
{
private Vector3 pos ;
void Update ()
{
if(Input.GetKey(KeyCode.UpArrow))
{
pos = this.transform.position;
pos.z +=.1f;
this.transform.position = pos;
}
if(Input.GetKey(KeyCode.DownArrow))
{
pos = this.transform.position;
pos.z -=.1f;
this.transform.position = pos;
}
if(Input.GetKey(KeyCode.LeftArrow))
{
pos = this.transform.position;
pos.x -=.1f;
this.transform.position = pos;
}
if(Input.GetKey(KeyCode.RightArrow))
{
pos = this.transform.position;
pos.x +=.1f;
this.transform.position = pos;
}
}
}
0 Comment(s)