- Home /
How do I move an obstacle in relation to a sloped line? (2D)
I have the following code:
using UnityEngine; using System.Collections;
public class obstacleController : MonoBehaviour {
private float xVal;
public float speed;
private float yMove;
// Use this for initialization
void Start () {
xVal = transform.position.x;
}
// Update is called once per frame
void Update () {
xVal = xVal - speed;
yMove = xVal * -0.17857687905f;
transform.Translate(xVal - speed, yMove);
print (yMove);
print (xVal);
}
}
-0.17857687905f is the slope of the line I'm trying to get the object to move parallel to
speed is set to .25
If I use transform.position (blah blah) and I put 2 objects with this script at different y values and run the script, it will teleport them to the same y value and move parallel to the line).
When I use the method shown above, the objects just ping off into the middle of nowhere.
I'm really stuck and kind of new to Unity. Please help. :)
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
2D movement with transform.translate and collision detection...? 1 Answer
access instantiated object variable 0 Answers
How to check if an int's value has decreased by 1? 1 Answer
Multiple Cars not working 1 Answer