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
0
Question by Will Poole · Dec 07, 2010 at 11:36 AM · movementgameobject

Trying to stop object snapping to a position

Hi i have got a problem that i can't work out how to fix without break my code. i was wondering if anyone could possibly help change my code below.

What this code does, is that tells the game objects to go to their end points, which are empty game object that act as reference points in virtual space, by a click of a GUI button.

At the moment the code works but it snaps the object to their end point instead of flowing (so you get to see the movement from A to B)

What i want to achieve is this "flow movement" and i have been told in the past that this can be done with delta.time or something on the lines of that.

I would be grateful for any help thank you.

var InnerCog : GameObject; var InnerRing : GameObject; var LeftScrew : GameObject; var OuterCog : GameObject; var OuterNut : GameObject; var OuterRing : GameObject; var RightScrew : GameObject; var Shaft : GameObject; var InnerShaftRing : GameObject; var InnerTopRing : GameObject; var SecondShaftRing : GameObject; var Top : GameObject;

var ICEnd : Transform; var IREnd : Transform; var LSEnd : Transform; var OCEnd : Transform; var ONEnd : Transform; var OREnd : Transform; var RSEnd : Transform; var SEnd : Transform; var ISREnd : Transform; var ITREnd : Transform; var SSREnd : Transform; var TEnd : Transform;

function OnGUI () { if (GUI.Button (Rect (760,10,90,25), "Explode")) { InnerCog.transform.position = ICEnd.transform.position; InnerRing.transform.position = IREnd.transform.position; LeftScrew.transform.position = LSEnd.transform.position; OuterCog.transform.position = OCEnd.transform.position; OuterNut.transform.position = ONEnd.transform.position; OuterRing.transform.position = OREnd.transform.position; RightScrew.transform.position = RSEnd.transform.position; Shaft.transform.position = SEnd.transform.position; InnerShaftRing.transform.position = ISREnd.transform.position; InnerTopRing.transform.position = ITREnd.transform.position; SecondShaftRing.transform.position = SSREnd.transform.position; Top.transform.position = TEnd.transform.position; } }

//Written by Will Poole "Explode Script" 6/12/10

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Statement · Dec 07, 2010 at 04:06 PM

Try this code. You need to call StartCoroutine with Move for each pair of object you got.

function OnGUI () { if (GUI.Button (Rect (760,10,90,25), "Explode")) { var speed = 1.0f; StartCoroutine( Move( InnerCog, ICEnd, speed ) ); StartCoroutine( Move( InnerRing, IREnd, speed ) ); // And so on... } }

function Move(GameObject from, Transform to, float speed) { while (from.transform.position != to.position && speed > 0) { from.transform.position = Vector3.MoveTowards(from.transform.position, to.position, speed * Time.deltaTime); yield; } }

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
avatar image
0

Answer by skovacs1 · Dec 07, 2010 at 03:18 PM

Assigning the position like this doesn't even show the movement from A to B. Assigning the position simply jumps to B. Flowing? I'm not sure exactly what you mean by this as we aren't talking about a fluid simulation or anything, but I'll assume that you mean moving or moving with easing.

A few notes:

  • No need to store the GameObject if you don't use it.
  • You don't need to get the transform when you're already storing it.
  • You shouldn't do your movement in OnGUI as that is called multiple times per frame. Put movement in an Update function if you can.

To move to the position from A to B, you would probably want to use Lerp. To get constant movement, you would store the start position as well as the end. Something like:

var subject : Transform; var start : Transform; var end : Transform; var speed : float = 0.5f; private var control : float = 1.0f;;

function OnGUI() { if(control >= 1.0f && GUI.Button(Rect(760,10,90,25),"Move") control = 0.0f; }

function Update() { if(control < 1.0f) { control += Time.deltaTime speed; subject.position = Vector3.Lerp(start.position,end.position,control); ///If you want to add easing subject.position = Vector3.Lerp(start.position,end.position, Mathf.SmoothStep(0.0f,1.0f,control));/ ///Swap start and end to go back and forth //There are other ways to do it, but they involve clamping, etc. if(control >= 1.0f) { var temp : Transform = start; start = end; end = temp; }*/ } }

If you want to store the start without having to have another transform for it, you could do something like this:

var subject : Transform; var endPoint : Transform; var speed : float = 0.5f; private var control : float = 1.0f;; private start : Vector3 = Vector3.zero; private end : Vector3 = Vector3.zero;

function Start() { start = subject.position; if(endPoint) end = endPoint.position; }

function OnGUI() { if(control >= 1.0f && GUI.Button(Rect(760,10,90,25),"Move") control = 0.0f; }

function Update() { if(control < 1.0f) { control += Time.deltaTime * speed; subject.position = Vector3.Lerp(start,end,control); } }

To ease in only towards the end, you can Lerp from subject.position to the end.position, but then your movement is framerate dependent. When doing this, you typically want to lerp by some fixed amount as if you were to lerp with Time.deltaTime, when your frame takes a longer amount of time, your movement would move by larger amounts and for a frame that took 1 second, your subject would jump to the end.position. The other concern is that unless you use Time.deltaTime and a frame takes 1 second, your subject will never reach end exactly, but will be approaching it forever so you cannot compare the two positions for equality (which is generally not a safe idea anyways). To aleviate framerate dependencies, you can place the movement in FixedUpdate.

This setup doesn't seem very scalable. If you add or remove objects, you need to change the script. You might be better off having a control script and a movement script on the objects you want to move:

MoveButton.js (put this on the camera or something)

function OnGUI() {
    if(!Mover.moving && GUI.Button(Rect(760,10,90,25),"Move")
        Mover.moving = true;
}

Mover.js (put this on each object you want to move)

static var moving : boolean = false; var endPoint : Transform; var speed : float = 0.5f; private var control : float = 1.0f;; private start : Vector3 = Vector3.zero; private end : Vector3 = Vector3.zero;

function Start() { start = subject.position; if(endPoint) end = endPoint.position; }

function Update() { if(control < 1.0f) { control += Time.deltaTime * speed; subject.position = Vector3.Lerp(start,end,control); if(control >= 1.0f) { control = 0.0f; moving = false; } } }

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

No one has followed this question yet.

Related Questions

Why are these object passing through each other? 1 Answer

How to let a GameObject move? 2 Answers

Object moving OnTriggerEnter, please help?! 2 Answers

Interaction around an gameObject 1 Answer

Coroutine fires once after destroying gameobjects in a scene and recreating new objects 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