No matter what the value of the speed variable is, my object travels at the same speed. This has been eating at my brains for a while now. Multiplying with only Time.deltaTime only too doesn’t work. What am I doing wrong ?
public float speed = 0.000000002F;
void Update() {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate(touchDeltaPosition.x * speed * Time.deltaTime, touchDeltaPosition.y * speed * Time.deltaTime, 0);
}
There is no animation code here.
Instead you are instantaneously teleporting the object to a new location which lis the one currently under your finger.
That location is being slightly modified by speed but my guess is its being balanced by a very small number of ms.
If you aren’t actually looking for animation, but just want it to stay under your finger, then DONT multiply the touch position by anything.
If you want it to slide slowly to your finger, you will have to implement animation code.
Jeff is absolutely correct.
It seems you want to calculate a movement direction based on the touchDelta. This is done by normalizing the touchDelta to create a directional vector, then multiply that by speed and Time.deltaTime :
public float speed = 2.0f;
void Update() {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
Vector2 movePosition = touchDeltaPosition.normalized * speed * Time.deltaTime;
transform.Translate(movePosition.x, movePosition.y, 0);
}
}
I don’t have a touch device to test the above, but this script shows the theory :
#pragma strict
public var speed : float = 2.0;
private var myTransform : Transform;
private var lastPos : Vector2;
private var currentPos : Vector2;
function Start()
{
myTransform = transform;
}
function Update()
{
#if UNITY_EDITOR
if ( Input.GetMouseButtonDown(0) )
{
OnTouchBegan( new Vector2( Input.mousePosition.x, Input.mousePosition.y ) );
}
else if ( Input.GetMouseButton(0) )
{
OnTouchMoved( new Vector2( Input.mousePosition.x, Input.mousePosition.y ) );
}
else if ( Input.GetMouseButtonUp(0) )
{
OnTouchEnded( new Vector2( Input.mousePosition.x, Input.mousePosition.y ) );
}
#else
if ( Input.touchCount > 0 )
{
if ( Input.GetTouch(0).phase == TouchPhase.Began )
{
OnTouchBegan( Input.GetTouch(0).position );
}
else if ( Input.GetTouch(0).phase == TouchPhase.Moved )
{
OnTouchMoved( Input.GetTouch(0).position );
}
else if ( Input.GetTouch(0).phase == TouchPhase.Ended || Input.GetTouch(0).phase == TouchPhase.Canceled )
{
OnTouchEnded( Input.GetTouch(0).position );
}
}
#endif
}
function OnTouchBegan( thePos : Vector2 )
{
lastPos = thePos;
currentPos = thePos;
}
function OnTouchMoved( thePos : Vector2 )
{
currentPos = thePos;
var moveDirection : Vector2 = ( currentPos - lastPos ).normalized;
var movePosition : Vector2 = moveDirection * speed * Time.deltaTime;
myTransform.Translate( movePosition.x, movePosition.y, 0 );
lastPos = thePos;
}
function OnTouchEnded( thePos : Vector2 )
{
}