i am changing the position of the my gameobject by reacging it’s transform component.I want to take a soft passing but its passing very fast(like beamed up).
So is there any idea to make it soft? Here is the code, thanks.
void Update () {
if (Input.GetTouch(0).phase==TouchPhase.Began)
{
if (i == 1)
{
GMaincar.transform.position = new Vector3(-0.72f, GMainpatates.transform.position.y, 0);
i--;
}
else if (i == 0)
{
GMaincar.transform.position = new Vector3(0.72f, GMainpatates.transform.position.y, 0);
i++;
}
}
So, are you simply trying to toggle between two different positions on the screen, but instead of having it instantly jump back and forth, you want it to smoothly go from one point to the other over, say, a full second?
You need to determine where the touch is on the screen and multiply the direction of the player with the normal of the axis you want to move in. If the touch is at Vector2(-2,3), multiply by -1.
So, I’m still not entirely sure on the mechanics of how you want things to work, but this should get you a bit closer to your goal. I’ve tried to make it as readable as possible and comment everything I did, but if you have any questions, let me know. I’ll try to explain it in more detail.
You are going to also need a few control variables, which I declare right above the Update() function, but you will probably want to move them near any other variables that you declare in this script.
bool targetingLeft = false; //This is your 'i variable' replacement since I can't see where it is coming from
Vector3 target; //GMaincar will need a target position to move towards later on
Vector3 startPosition; //This is the position of GMaincar when a target is set (so we have a constant movement)
float delta = 0.0f; //This is the percent of the move that is done. When it hits 1, we will have reached the target
bool hasTarget = false;
void Update ()
{
if (Input.GetMouseButtonDown(0))//Input.GetTouch(0).phase==TouchPhase.Began)
{
//Since the screen was touched, or the mouse clicked in this case, we need to assign a target
//So that you can move back and forth anytime, let's flip the target each time the screen is touched
targetingLeft = !targetingLeft; //Makes 'targetingLeft' the opposite, thus flipping it
//If we are targeting the left side (the -0.72 value) set that as the target
if(targetingLeft == true)
target = new Vector3(-0.72f, GMaincar.transform.position.y, 0);
else //If 'targetingLeft' is false, we will assign the right side as the target
target = new Vector3(0.72f, GMaincar.transform.position.y, 0);
startPosition = GMaincar.transform.position;
hasTarget = true; //Set the 'hasTarget' flag so we know that GMaincar should move later on
delta = 0.0f; //Also reset delta if it has changed
//Now that we have a target, we can move towards that target in the standard update cycle
}
//Outside of the Input section
if(hasTarget)
{
//Add the Time.deltaTime to the delta variable to update the 'percent of move that is complete'
delta = delta + Time.deltaTime;
//Move GMaincar towards the target
GMaincar.transform.position = Vector3.Lerp(startPosition, target, delta);
if(delta >= 1.0f)
{
//No longer needs to move, so set hasTarget to false
hasTarget = false;
}
}
}