- Home /
Changing the speed variable does not change the speed of the motion of my touch based object.
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);
}
$$anonymous$$y first thought is that 0.000000002F is a really small number to use for movement, you should firstly try using a much larger number to begin with and rather scale downwards from that point.
Also:
I would assume that multiplying 0.000000002F by Time.deltaTime could result in a float that simply is zero if 0.000000002F isn't changed to zero in the first place.
An example deltaTime when running at 60 FPS would be 0.016 (16ms per frame) and multiplying 0.016 by your speed variable gives us 0.000000000032 Taken that float has a precision of 7 digits and 0.000000000032 has 12 digits I would assume that 0.000000000032 becomes zero.
However I'm not certain about all of this, I just did some quick research on floating point numbers, you could try using a double for speed and see where that takes you.
For a small movement of touch drag on the screen, my object seems to be having a lot faster motion. For example, if I drag on the screen for 1cm, the object moves a lot more than 1cm. To $$anonymous$$imize this increment in the speed, I'm trying to multiple the movement by the speed float variable, but no amount of value seems to be affecting the speed as it seems to be the same.
If you want it to be under your finger, don't multiply the mouse position by anything.
Not multiplying it by anything is still faster than I want it to be.
Answer by Jeff-Kesselman · May 06, 2014 at 04:45 PM
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.
Well that code makes the object travel on an X,Y axis, when a touch is dragged on the surface of the device. And it is not teleporting to any new location. So, if I touch the surface and drag upwards, my object will move up, and so on and so forth for any kind of path that I make with my finger, while pressing down on the screen.
NO, sorry, you are wrong.
What that code does is teleport the object to under your finger (approximately), as you move your finger, it teleports to the new finger location.
The "animation' is co$$anonymous$$g from you moving your hand.
That is quite odd, since I have this exact code working as I described it to be working in Unity right now as I type this. I've been using it for the past 2 days now.
Yes and it is doing exactly what you describe, which is what you don't want.
I've explained to you WHY its doing that.
If you want to argue rather then listen and learn, thats your issue.
//plonk
Firstly, I DON'T want it to teleport to wherever my finger touches. Secondly, it is NOT teleporting to wherever my finger touches currently. So this is good. For a small distance that I touch-dragged on the screen, the amount the object moves is more than I want it to be, which is my sole concern here. I'm trying to multiply that with the speed variable but the speed doesn't change. So, I'd like to know why is this happening and what can I do to make the distance travelled by my object less than what it is travelling right now. I don't see what would make you assume that I'm arguing when I'm trying to elaborate my situation better so it is clearer for readers here to understand.
Answer by AlucardJay · May 07, 2014 at 04:10 AM
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 )
{
}
Your answer