Unity 2d Move Rowards Direcrion and Continue
- Spaces
- Default
- Help Room
- META
- Moderators
-
- Topics
- Questions
- Users
- Badges
- Home /
0
Question by startas · Dec 31, 2014 at 07:07 PM ·
Move object to target direction in 2D
How can i move my object to my target direction ? I have two points that i know - bullet(x, y) and enemy(x, y), now moving is done by
bullet.transform.position = Vector3.MoveTowards (bullet.transform.position, target.transform.position, bullet.speed);
but this only moves towards some fixed position. I also can move bullet by just adding some values to position, but i need a way to calculate movement towards direction of fixed enemy position, because i need the bullet not to stop moving when it reaches enemy.
2 Replies
· Add your reply
- Sort:
1
Answer by TheDJBuntin · Jul 02, 2017 at 02:12 PM
Sorry for necro'ing, but I came across this while googling for answers as its quite high in the search results. it looks like ramp is along the right tracks but there are errors in his code that'll confuse inexperienced users.
Below is a snippet of what I got working.
private Vector3 myTarget = new Vector3(0.0f, 0.0f, 0.0f); // Example Target Position Vector private float speed = 3.0f; void Update() { // Movement float distance = Vector3.Distance(transform.localPosition, myTarget); if (distance > 1.0f) { Vector3 heading = myTarget - transform.localPosition; //use transform.position if you are using world space Vector3 direction = heading / distance; // Normalizing float deltaSpeed = speed * Time.deltaTime; transform.Translate(direction.x * deltaSpeed, direction.y * deltaSpeed, direction.z * deltaSpeed, transform.parent.transform); } } For the snippet im just using an example position of 0, 0, 0 for myTarget, you can set this however you like. Another bit to point out is "transform.parent.transform" - this is element is the Transform relativeTo, I chose transform.parent.transform because in what I was working on my object is a child of another, so I want its translation to be relative to its parent rather than the world, if you just want to use world space use as ramp suggested with Space.World.
You can do the same thing in 2D by changing direction to a Vector2 and replacing direction.z * deltaSpeed with 0.0f.
Since it was OPs question: here is a snippet of what you would do if you didnt want it to stop once it reaches its destination, i've also made this one to world space to show what you would do for that.
private Vector3 myTarget; private Vector3 direction; private float speed = 3.0f; void Start() { // Establish target & direction myTarget = new Vector3(0.0f, 0.0f, 0.0f); direction = myTarget - transform.position; //fixed position direction.Normalize(); } void Update() { // Move in direction of target. float deltaSpeed = speed * Time.deltaTime; transform.Translate(direction.x * deltaSpeed, direction.y * deltaSpeed, direction.z * deltaTime, Space.World); } Hope this helps anyone who stumbles on this.
0
Answer by ramp · Jan 01, 2015 at 11:39 AM
Hi,
You don't need to know the distance. Also you should have a Speed value for your Game Object and you don't need to stop bullet at target position. Then you could do the following:
Calculate the direction vector
Normalize direction vector
Create a moving factor using Speed and Time.deltaTime values
Use Transform.Translate function to move the game object
Something like this:
public GameObject Target;
public float Speed;
void Update() {
Vector2 direction = Target.transform.position;
direction.Normalize();
float factor = Time.deltaTime * Speed;
this.transform.Translate(direction.x factor, direction.y factor, direction.z * factor, Space.World);
}
Your answer
Welcome to Unity Answers
If you're new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.
Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.
Check our Moderator Guidelines if you're a new moderator and want to work together in an effort to improve Unity Answers and support our users.
Follow this Question
Related Questions
Source: https://answers.unity.com/questions/865854/move-object-to-target-direction-in-2d.html
I still have a problem - even if i calculate normalized vector and move bullet by adding that vector to bullet position, speeds doesnt match, i.e. bullet1(5, 7), its normalized vector(-0.8, 0.7), so after one move bullet will be in position(4.2, 7.7), so distance that it moved is square root of(0.8*0.8 + 0.7*0.7), or sqrt(1.13), and bullet2(9, 20), its normalized vector(0.2, 1), so after one move bullet will be in position(9.2, 21), so distance that it moved is square root of(0.2*0.2 + 1*1), or sqrt(1.04), so sqrt(1.13) is not equal to sqrt(1.04). Is this because of some limitations or i missed something ? Because now bullets fly at correct direction, but at different speeds.