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 Mz3D · Jul 12, 2012 at 10:56 PM · colliderrelativemathf.clampabsolute

Relative and absolute values problem

Hello everyone. I have an object which is randomly instantiated on the x axis at a position between x = - 4 and x = 4. Once it is instantiated i would like it to start a loop movement between x = - 4 an x = 4. I've used the Mathf.PingPong function but it doesn't work since this function creates a movement from the relative starting position to another relative point and back to the starting position. I need to set the absolute world values - 4 and 4. I tryed using Mathf.Clamp as argument for the Mathf.PingPong function but it didn't work either. This is the code i'm using:

 transform.position = transform.position + Vector3(Mathf.PingPong(Time.time, 8), 0, 0);

I thought about a solution but something doesn't work: i tryed to create two planes with colliders and rigid bodies and put them at x = - 4 and x = 4, i applied a tag "Limit" to them. Then i wrote this code and attached to the object that should move between the two x values:

 function OnTriggerEnter (col : Collider){
     if(col.gameObject.tag == "Limit"){
      transform.position.x = -transform.position.x;
     }
 }

I though that this way when the object hits a plane should change its x direction and start moving the opposite way. But when i start the game and the object hits a plane doesn't happen anything at all. Something must be wrong, can you help me?

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
2

Answer by Eric5h5 · Jul 13, 2012 at 12:16 AM

You can create a variable and update that via Time.deltaTime.

 private var pos : float;
 
 function Start () {
  pos = Random.Range (0.0, 8.0);
  transform.position.x = pos - 4.0;
 }
 
 function Update () {
  pos += Time.deltaTime;
  transform.position.x = Mathf.PingPong (pos, 8.0) - 4.0;
 }
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 Ingen · Jul 13, 2012 at 12:18 AM

take a look at

http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Lerp.html

http://docs.unity3d.com/Documentation/ScriptReference/Transform.Translate.html

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

6 People are following this question.

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

Related Questions

Specifying a game object by height or width or length instead of localScale 1 Answer

Internal collisions 1 Answer

how to convert absolute path to relative Application.DataPath 1 Answer

C# Check if Collider2D[] Doesn't contain Scripted GameObject 1 Answer

Attempt counter 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