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 Petras84 · Jul 21, 2012 at 06:11 PM · collisiontriggerlerp

Problem with lerping several objects to positions.

I have 4 game objects and 4 sockets. whenever i activate "row 1", i want each game object to jump to the adjacent socket and stay there until i activate "row 1" again. (object in socket 4 jumps to socket 1).

the socket and socketpos variables are linked to the same objects. It was necessary as Lerp asks for Transform, and Trigger asks for GameObject.

The following script is attached to each socket:

private var row; var Socket1 : GameObject; var Socket2 : GameObject; var Socket3 : GameObject; var Socket4 : GameObject; var Socket1pos : Transform; var Socket2pos : Transform; var Socket3pos : Transform; var Socket4pos : Transform

function OnTriggerStay (other : Collider) { if (row == 1) { if(other.gameObject == Socket1) { transform.position = Vector3.Lerp(transform.position, Socket2pos.position, Time.deltaTime *5); } if(other.gameObject == Socket2) { transform.position = Vector3.Lerp(transform.position, Socket3pos.position, Time.deltaTime *5); } if(other.gameObject == Socket3) { transform.position = Vector3.Lerp(transform.position, Socket4pos.position, Time.deltaTime *5); } if(other.gameObject == Socket4) { transform.position = Vector3.Lerp(transform.position, Socket1pos.position, Time.deltaTime *5); } } } The problem is; the objects just spin around uncontrollably, and i have no idea how to get them to just jump to the adjacent socket and stay. I have been stuck on this for days now, so i hope you pro's can help a noob scripter out! Thanks!

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 Eric5h5 · Jul 21, 2012 at 07:00 PM 0
Share

Use arrays ins$$anonymous$$d of multiple separate variables.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ScroodgeM · Jul 21, 2012 at 06:52 PM

transform.position = Vector3.Lerp(transform.position, Socket1pos.position, Time.deltaTime *5);

replace it with

Vector3 offsetNeeded = Socket1pos.position - transform.position;
float moveSpeed = 1f; // insert move speed here
if (offsetNeeded.sqrMagnitude > moveSpeed * moveSpeed * Time.deltaTime * Time.deltaTime)
{
  offsetNeeded = offsetNeeded.normalized * moveSpeed * Time.deltaTime;
}
transform.position += offsetNeeded;

not tested

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 aldonaletto · Jul 22, 2012 at 05:46 PM

This logic seems wrong: supposing this script is added to the objects and the sockets are the triggers, each object will start moving to the next socket when row becomes 1, but will stop when the object leaves the current trigger (if the triggers don't overlap) or start a weird movement when entering the next socket trigger (if the triggers overlap).
I would use a different logic: in the object script, find all sockets at Start and populate an array, then control the object position by the array index - like this (object script):

var numSockets: int = 4; // how many sockets you have var curSocket: int = 0; // define each object's socket number in the Inspector var speed: float = 0.5; // speed to move to the next socket

private var sockets: GameObject[];

function Start(){ // allocate the sockets array: sockets = new GameObject[numSockets]; // populate it with objects named "Socket0", "Socket1", "Socket2" etc. for (var i=0; i< numSockets; i++){ sockets[i] = GameObject.Find("Socket"+i); } }

function Update(){ // find the actual curSocket position: var pos = sockets[curSocket].transform.position; // move towards it (does nothing if already at that position): transform.position = Vector3.MoveTowards(transform.position, pos, speed * Time.deltaTime); }

function NextSocket(){ // function to move to the next socket: curSocket++; // increment socket number // if passed the last socket, cycle to the first one: if (curSocket >= numSockets) curSocket = 0; } When you want to make the objects jump to the next socket, call NextSocket in all objects. The easiest way to do that is to create an empty object (let's call it "Objects"), reset its position and rotation and child the four objects to it, then add the control script below, which will call the function NextSocket in each child via SendMessage:

function JumpToNextSocket(){
  for (var child: Transform in transform){
    child.SendMessage("NextSocket");
  }
}
You must call JumpToNextSocket once in the control script when you want the objects to move to their respective next sockets
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

7 People are following this question.

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

Related Questions

activate gui on trigger enter 1 Answer

RigidBody - GameObject moves but mesh stays! 1 Answer

Inverse 2D Collider, is this possible? 0 Answers

Best practice for OnTriggerEnter detection 1 Answer

Performance: How expensive are Trigger Collisions? How many collision layers should be used? 1 Answer


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