Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by siddharth3322 · Dec 13, 2014 at 07:47 AM · movementdirectionangle

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Dec 13, 2014 at 08:55 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tfreeman3000 · Apr 12, 2016 at 12:27 AM 0
Share

Just what I needed thanks

avatar image Hanh · Jan 15, 2018 at 07:24 AM 0
Share

it's exactly what i need. 1$$anonymous$$ thanks

avatar image
1

Answer by hav_ngs_ru · Dec 13, 2014 at 08:45 AM

 transform.position += transform.forward * someSpeed * Time.deltaTime;
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image siddharth3322 · Dec 13, 2014 at 08:58 AM 0
Share

I don't want to move in straight path only means top, bottom, left or right. I also want to move diagonally also.

avatar image
1

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;
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image darkcookie · Dec 13, 2014 at 09:36 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges