- Home /
Transform.position can't be changed smoothly.
Hello UnityAnswers, I have a problem. Not sure if small or huge tho! I have a code that allows an object to change it's position.x when you swipe to a direction. The problem that i have here is, I can't make it work to go from position.x = 0 to position.x = 4 smoothly. For example, when you swipe right, "Player" instantly goes to position.x = 4. I tried the code : this.transform.position.x = 4 * Time.deltaTime * 2;
But nothing happens. Here is my full code written in JS.
#pragma strict
var firstPressPos : Vector2;
var currentSwipe : Vector2;
var current_pos : int;
var speed : int;
var can_speed : boolean;
function Start () {
current_pos = 2;
speed = 3;
speed_up();
}
function Update () {
this.transform.position.z = this.transform.position.z + speed * Time.deltaTime * 2;
if (Input.GetMouseButtonDown(0))
{
firstPressPos = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
{
currentSwipe = Input.mousePosition - firstPressPos;
if (currentSwipe[0] + 150 > 0)
{
if(current_pos == 3){
return;
}if(current_pos == 2){
this.transform.position.x = 4;
current_pos = 3;}
if(current_pos == 1){
this.transform.position.x = 0;
current_pos = 2;}
}
if (currentSwipe[0] + 150 < 0)
{
if(current_pos == 1){
return;
}
if(current_pos == 2){
this.transform.position.x = -4;
current_pos = 1;}
if(current_pos == 3){
this.transform.position.x = 0;
current_pos = 2;}
}
}
}
function speed_up(){
for(var i = 0; i < 10; i++){
yield WaitForSeconds (10);
if(speed < 21){
speed = speed + 6 ;
InvokeRepeating("speed_up", 0, 2);
can_speed = false;}else{return;}
}
}
Thanks for your time!
Answer by Hexer · Aug 05, 2015 at 10:39 AM
You could make use of Vector3.MoveTowards.
http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html
Or you could make a small dash animation that gives the illusion that the player moved with an incredible high speed.
I find the latter cooler, but it is up to you.
Answer by $$anonymous$$ · Aug 05, 2015 at 01:31 PM
You can use vector3.lerp for smooth movement.
http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html
This may help you.
Your answer
Follow this Question
Related Questions
Slower Ladder 2 Answers
How to smooth movement object in one-axis? 1 Answer
Smooth Rotation Help 1 Answer
How Can I Make The Parkour Moves Move Smoothly Instead Of Instantly Teleport? 1 Answer
Change object position on trigger enter 3 Answers