This is my script so far
#pragma strict
var right : Transform;
var left : Transform;
var count : int = 0;
function Start () {
transform.position = right.position;
}
function Update () {
}
function OnCollisionEnter ( collision : Collision) {
if( collision.gameObject.name == “Right”)
transform.position = Vector3.Lerp(transform.position, left.position, Time.time );
if( collision.gameObject.name == “Left”)
transform.position = Vector3.Lerp(transform.position, right.position, Time.time );
}
So the problem is, the sphere starts at the “right” wall and moves smooth 2 the left wall, but then the sphere starts 2 flip around instead of going back smoothly to the “right” wall.
I would appreciate some help right there
Basen1
July 25, 2017, 4:38pm
2
function OnCollisionEnter ( collision : Collision) {
if( collision.gameObject.name == “Right”)
transform.position = Vector3.Lerp(transform.position, left.position, Time.time );
else if( collision.gameObject.name == “Left”)
transform.position = Vector3.Lerp(transform.position, right.position, Time.time );
}
Try that please
Basen1
July 25, 2017, 4:39pm
3
Also check that Left and RIght Transform are the correct walls so that Right isn’t the left wall, that would explain it trying to lerp into itself
still the same problem. like the sphere should run between those walls smoothly, the first run from left to the right is smooth, but then ist just bouncing and not going smoothly ;/
or maybe is there any other option i can use instead of vetor3.lerp? i already tried to work with the rigidbody and add forces forwards, but its not working as well. it sounds so simple but i have no clue
Basen1
July 25, 2017, 4:57pm
7
Can you please use a fixed float instead of Time.time? just say 10f
tried it, but its still not working. do you have an idea how to make an object move from point A to B without vector3.lerp or using the transform.translate stuff?
Basen1
July 25, 2017, 5:08pm
9
Well the thing is you need to put the Lerp in the update function or a coroutine
Basen1
July 25, 2017, 5:12pm
10
#pragma strict
public var right : Transform;
public var left : Transform;
var count : int = 0;
var goLeft : boolean = false;
var speed : int = 4;
function Update(){
if(count <= 20){
if(goLeft){
transform.position = Vector3.Lerp(transform.position,left.position, Time.deltaTimespeed);
}
else if(!goLeft){
transform.position = Vector3.Lerp(transform.position,right.position, Time.deltaTime speed);
}
}
}
function OnCollisionEnter(collision:Collision){
if(collision.gameObject.name == “Right”){
goLeft = true;
count++;
}
else if(collision.gameObject.name == “Left”){
goLeft = false;
count++;
}
}
1 Like
Basen1
July 25, 2017, 5:14pm
11
That should work, you might have to multiply the Time.deltaTime with a float to make it go a bit faster though
Vector3.MoveTowards is similar to Vector3.Lerp aswell takes the same arguments/parameters
#pragma strict
var right : Transform;
var left : Transform;
var count : int = 0;
var speed : int = 4;
private var goLeft : boolean;
function Start () {
transform.position = right.position;
}
function Update(){
if(goLeft== true){
Vector3.Lerp(transform.position,left.position, Time.deltaTime);
}
else{
Vector3.Lerp(transform.position,right.position, Time.deltaTime);
}
}
function OnCollisionEnter(collision:Collision){
if(collision.gameObject.name == “Right”){
goLeft = true;
}
else if(collision.gameObject.name == “Left”){
goLeft = false;
}
}
did i used it right? well now its moving from right to left but then it stops at the left wall and not going back to the right.
Basen1
July 25, 2017, 5:23pm
13
I updated my answer and you can remove the Start function
You can just copy my answer above as the entire function
thank you a lot ! it works, god bless you :)!
Basen1
July 25, 2017, 5:35pm
15
Sorry, just tested it, there were some syntax errors, I prefer C# I suggest you start learning it aswell they are removing Unityscript in 2017.3
Glad you got it working
Also I suggest MoveTowards if you don’t need the easing in/out from Lerp
im working with unity since 7 months now, but i only worked with javascript, but yea i will check out C# soon. have a great day