To change object Position Smoothly
hey Firends! i am working on a game in which i want my object to change its position verticaly after trigger. i have done this, but when it is triggered it jump suddnely. i want it to change its position smoothly using some time And Here is my Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Target : MonoBehaviour {
// Use this for initialization
public Text text;
public int maxHits = 3;
public int CountScore=0;
public float moveAmount;
public Vector2 TargetPosition;
public Ball ball;
static float t = 0.0f;
void Start(){
TargetPosition = transform.position;
Debug.Log ("first position" + TargetPosition);
}
void OnTriggerEnter2D (Collider2D Collision) {
CountScore++;
Debug.Log (CountScore);
//Moving Target position According to hits
if (maxHits!=null) {
TargetPosition.y += moveAmount;
MoveTargetPosition ();
maxHits--;
}
}
//move target function
void MoveTargetPosition(){
Debug.Log ("my position" + TargetPosition);
transform.position = new Vector2(TargetPosition.x, this.TargetPosition.y);
}
}
Please help and thanx in Advance.
Answer by Backside2357 · Dec 29, 2017 at 12:15 PM
Maybe the Vector3.Lerp(vectorA, vectorB, t)
function is what you are looking for? The parameter t is a value between 0 and 1, where the Lerp function returns vectorA for t=0 and vectorB for t=1 and something inbetween (linear) for inbetween t values. The t value could then be computed using Time functions.
Well I would delete the $$anonymous$$oveTargetPosition function and ins$$anonymous$$d would set a bool value where the function was called:
do$$anonymous$$ove = true;
And also set the variable t to 0:
t = 0;
It would also be necessary to make a copy of the current position:
CurrentPosition = transform.position;
Then, in the standard $$anonymous$$onoBehaviour Update function you write something like this:
if( do$$anonymous$$ove )
{
t += Time.deltaTime;
if(t >= duration)
{
transform.position = TargetPosition;
do$$anonymous$$ove = false;
}
else
{
transform.position = Vector2.Lerp(CurrentPosition, TargetPosition, t / duration);
}
}
duration is a constant you need to define and set to the value you want.
Thanx For Your Time it Did'nt Worked. the object moves same like before. it jumps suddenly. i want to move the object smothly within a duration
That is strange. But I found the time to create a simple example script:
Add a cube to your scene
Create a C# script with the name Smooth$$anonymous$$ove
Replace the content of the script with the code posted below
Attach the script to the cube
In Play$$anonymous$$ode, click the "Do $$anonymous$$ove" checkbox and play with the "Duration" value
Understand how the script works and adapt it to your needs
Code (Smooth$$anonymous$$ove .cs):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Smooth$$anonymous$$ove : $$anonymous$$onoBehaviour {
public bool do$$anonymous$$ove = false;
public float duration = 2.0f;
private float t = 0;
private Vector3 originPosition;
private Vector3 targetPosition;
// Use this for initialization
void Start () {
originPosition = transform.position;
targetPosition = originPosition + new Vector3(10, 0, 0);
}
// Update is called once per frame
void Update () {
if (do$$anonymous$$ove)
{
t += Time.deltaTime;
if (t >= duration)
{
transform.position = targetPosition;
do$$anonymous$$ove = false;
}
else
{
transform.position = Vector3.Lerp(originPosition, targetPosition, t / duration);
}
}
else
{
t = 0;
transform.position = originPosition;
}
}
}
@Backside2357 Thanx For your time it hellped me. now the object is moving smoothly Thanx Again :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612145433im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to All instantiated objects to change position if any object hits defined border 2 Answers
how do I use an Idle animation clip in a 2d blend tree? 0 Answers
Can't jump while running, and animation won't transition 0 Answers
Hello, help with animation between scenes 0 Answers
Transition Between Animations Delay 0 Answers