- Home /
Question by
SpectralEdge · Oct 16, 2012 at 02:42 AM ·
c#inputitween
Limit distance from start while using iTween
I am grabbing a block and while using a and d moving it right and left along the x axis. But I am trying to limit the movement to a certain distance from the start position.
Having some trouble as every time I get to the limit it stops but then I can't reverse directions.
public float speed;
public Vector3 limitFromStartPosition;
Vector3 startPosition;
void Start ()
{
startPosition = transform.position;
}
void Update ()
{
//TODO remake code to work on multiple axis.
float horizontal = Input.GetAxis ("Horizontal");
if (limitFromStartPosition.x >= Vector3.Distance (startPosition, transform.position))
{
if (Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.D))
{
iTween.MoveBy (gameObject, iTween.Hash ("x", horizontal*speed, "easeType", iTween.EaseType.linear, "isLocal", true));
}
//Spring a bit when we let go
if (Input.GetKeyUp (KeyCode.A) || Input.GetKeyUp (KeyCode.D))
{
iTween.MoveBy (gameObject, iTween.Hash ("x", horizontal, "easeType", iTween.EaseType.spring));
}
}
}
tried a few ways to fix it, none of them are working. I think because iTween is not giving the position yet so testing against the position before it stops moving won't work.
Work around suggestions?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613082419im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
iTween input with spring 1 Answer
Distribute terrain in zones 3 Answers
How to use Multi-Tap in New Input System for Running? 1 Answer
(C#) A better way to limit actions to once per button press? 2 Answers