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 zaragoza1 · Jul 23, 2014 at 06:00 AM · c#loopbegginer

[help]How to move an object left and righ (looping)

Hey! I'm new to coding, and I need to make a cube that goes from left to right and so on forever in c# (all coded), but i cant figure out how to do it. I know how to move it on one direction, but cant make it stop at a point and go the other direction.

Thank you very much!! :)

Comment
Add comment · Show 3
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 sethuraj · Jul 23, 2014 at 06:26 AM 0
Share

You can use $$anonymous$$athf.PingPong() to pingpong a value between 0 to X.Add a script to the cube with the following line.

 void Update()
 {
   transform.position=Vector3($$anonymous$$athf.PingPong(Time.time,10.0f), transform.position.y, transform.position.z);
 }

This will pingpong the cube between 0 and 10 in X axis while the Y and Z axis remains the same.Hope this helps.

avatar image zaragoza1 · Jul 23, 2014 at 09:53 AM 0
Share

Thank you very much!!! All of your ansewers helped me a lot. Im gonna share my results with my classmates tomorrow. Ty! :)

avatar image Fattie · Dec 23, 2015 at 03:42 AM 0
Share

Here ... http://answers.unity3d.com/answers/14287/view.html

6 Replies

· Add your reply
  • Sort: 
avatar image
15

Answer by robertbu · Jul 23, 2014 at 06:04 AM

I was going to close this question as a duplicate since similar questions have been asked and answered in the last couple of years, but in a few minutes of Googling I did not find a good fit for an answer to point you to. So here is a new answer. Whenever you have something that is cycling back and forth, whether it is position, rotation, color, etc, consider using Mathf.Sin() or Mathf.PingPong(). Usually for movement of some sort, Mathf.Sin() is a better choice. Here is a sample script:

 using UnityEngine;
 using System.Collections;
 
 public class BackAndForth : MonoBehaviour {
 
     public float delta = 1.5f;  // Amount to move left and right from the start point
     public float speed = 2.0f; 
     private Vector3 startPos;
 
     void Start () {
         startPos = transform.position;
     }
     
     void Update () {
         Vector3 v = startPos;
         v.x += delta * Mathf.Sin (Time.time * speed);
         transform.position = v;
     }
 }

Comment
Add comment · Show 3 · 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 calebheale · Jul 23, 2014 at 06:14 AM 1
Share

Very cool solution! I haven't seen this before but it makes sense considering we often model oscillation with sin waves.

avatar image kskjadav007 · Aug 19, 2017 at 10:06 AM 2
Share

how to apply rotation to this ?

avatar image amirl_unity · Oct 21, 2017 at 05:42 AM 0
Share

Thank you this worked!

avatar image
2

Answer by calebheale · Jul 23, 2014 at 06:10 AM

 int maxValue = 15; // or whatever you want the max value to be
 int minValue = -15; // or whatever you want the min value to be
 int currentValue = 0; // or wherever you want to start
 int direction = 1; 
 
 Update() {
    currentValue += Time.deltaTime * direction; // or however you are incrementing the position
    if(currentValue >= maxValue) {
       direction *= -1;
       currentvalue = maxValue;
    } else if (currentValue <= minValue){
       direction *= -1; 
       currentvalue = minValue;
    }
    transform.position = new Vector3(currentValue, 0, 0);
 }

Feel free to ask questions if any of this doesn't make sense. It might need to be slightly altered for your specific implementation.

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 RedDevil · Jul 23, 2014 at 06:04 AM

you can attach a rigibody to the cube and from code give it velocity to the left then you wait for a few seconds then give it velocity to the right and you put this in an infinite loop

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 kskjadav007 · Sep 14, 2017 at 10:07 AM

How to apply rotation for object that goes left and right on fixed frequency ????

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 verlinsolutionstesting · Sep 05, 2018 at 08:43 AM

How to make an object stop at particular position.

Hello,I am trying to create an simple AR app.What I want to do is, just wanted my plane object to move left from the image target and stop.But what happening is,the plane object is continuously moving to the left position. This is the code I have tried.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class vectors : MonoBehaviour {

 public GameObject plane;
 private Vector3 direction;
 public static Vector3 left;

 // Use this for initialization
 void Start()
 {
     transform.position += Vector3.left * Time.deltaTime;
 }

// Update is called once per frame void Update() { transform.Translate(-1 * Time.deltaTime, 0, 0); } }

Can you please resolve my problem. Thanks..

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
  • 1
  • 2
  • ›

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

12 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Nested Array? Populating a menu using a loop. 0 Answers

Using ForEach Loop to change variables on prefabs 1 Answer

Animation keeps playing 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