Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by PlatinumSkink · Apr 25, 2011 at 08:16 PM · rotationpositionobjectmovingtowards

Move object A towards object B

I want Object A to move towards Object B at a certain speed. If possible I'd also like Object A to rotate towards Object B. If possible, answer so simply that a person who did not even understand the Unity Reference page can understand. Thank you.

Current script on ObjectA.

var speed = 4.0; var ObjectB:GameObject;

function Update () {

}

Please do not link me to reference pages. Because they are never tailored to fit my situation. Thank you.

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 PlatinumSkink · Apr 27, 2011 at 04:36 PM 0
Share

I'd like to be able to move it in a definite speed. A single speed that does not change depending on how far from the player the object is. Is this possible?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Kourosh · Apr 25, 2011 at 08:26 PM

var speed = 4.0; var ObjectB:GameObject;

private var increment:float; private var rotation:Quaternion;

function Update () { if(increment <=1) increment += speed/100;

transform.position = Vector3.Lerp(transform.position, ObjectB.transform.position, increment);

//Add this block only if you want the rotation also be transitioned to objectB's rotation. var direction:Vector3 = ObjectB.transform.position - transform.position; rotation = Quaternion.LookRotation(direction); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, increment);

}

Lerp function converts a Vector3 to another while increment goes to 1 from 0.

Comment
Add comment · Show 6 · 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 Casper 1 · Apr 25, 2011 at 09:11 PM 0
Share

Couldn you just use transform.LookAt to achieve the rotation, or am I missing something?

avatar image Kourosh · Apr 25, 2011 at 09:14 PM 0
Share

Yes, it can be just LookAt, however i think LookAt makes a sudden rotation rather than having a smooth and gradual rotation. I was't sure which type he's looking for so I made the smoothed one :)

avatar image PlatinumSkink · Apr 26, 2011 at 04:36 AM 0
Share

Erm, thank you. This does indeed work. However, this movement is kind of uncontrollable. It starts out slow no matter what "speed" is on and then accelerates to an undefined speed that is not exactly how I want it. This also seems to get extremely faster should some distance be put between the objects. Is there some way that I could see some code that would only and simply move the object towards the other in a single definite velocity? I'll handle acceleration myself with changing the speed variable. If that is what the code is supposed to do, then I apologize. It does not.

avatar image PlatinumSkink · Apr 26, 2011 at 04:45 AM 0
Share

And what exactly does Vector3.Lerp do?

http://unity3d.com/support/documentation/ScriptReference/Vector3.Lerp.html

What is it this page is trying to tell me but that I clearly do not understand?

avatar image TheJables · Aug 10, 2012 at 03:59 PM 0
Share

Could you also just replace the increment variable with a "Time.deltaTime * Speed" to achieve the same effect?

Show more comments
avatar image
0

Answer by MikeTyke · Oct 02, 2016 at 09:30 AM

thank you

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Add force at position with respect to rotation 1 Answer

set child object position when playing the animation 0 Answers

Make object appear within camera view 0 Answers

Rotate a moving object 0 Answers

Rotate a thrown spear to Mouse Position (2d top down game) 0 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