- Home /
Move infinitely in targeted direction
For my game, I want to move my game object in specific direction but it has to travel any number of units.
At present I have following code for deciding direciton.
Vector3 direction = targetPlayer.position - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Using above code I have correction direction of object. But how to move game object infinitely on same direction founded?
Please give some opinions in this.
Be aware that floating point precision errors start to creep in around 20 km from the origin, will definitely be visible at 100 km. Physics starts to break down dramatically at around 250 km.
The solution to this is called floating origin. Something to keep in $$anonymous$$d if you want truly infinite distances.
Answer by siddharth3322 · Dec 15, 2014 at 05:32 AM
I have tested all answers suggested by other members but they are not worked at all. I finally figure out final answer of my question. Here I am pasting it to help other members.
public class InfiniteMove : MonoBehaviour
{
private Vector3 normalizeDirection;
public Transform target;
public float speed = 5f;
void Start()
{
normalizeDirection = (target.position - transform.position).normalized;
}
void Update()
{
transform.position += normalizeDirection * speed * Time.deltaTime;
}
}
Now object is moving in what ever direction target is exist.
Answer by hav_ngs_ru · Dec 13, 2014 at 08:45 AM
transform.position += transform.forward * someSpeed * Time.deltaTime;
I don't want to move in straight path only means top, bottom, left or right. I also want to move diagonally also.
Answer by darkcookie · Dec 13, 2014 at 08:48 AM
Do you mean something like this? if so just set "var direction" equal to the transform of the object you want
var direction = Vector3(0,1,0);
function Update () {
transform.position += direction * Time.deltaTime;
}
like This:
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
private var myTransform : Transform;
function Awake() { myTransform = transform; //cache transform data for easy access/preformance
}
function Update () { //move towards the Target
myTransform.position += target.forward * moveSpeed * Time.deltaTime;
}
Your answer
Follow this Question
Related Questions
How to edit Direction Angle using GetAxis 2.5D 8 direction movement 1 Answer
Making player face the direction it moves (using angles). 1 Answer
Making a character move automatically in one direction. 2 Answers
How to rotate a character facing direction of movement like sonic the hedgehog 2 Answers
Transform angle to grid position 2 Answers