Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by IansLJ · Sep 29, 2015 at 02:52 AM · c#errormovementtransformposition

Make object move back and forth

Hello fellow unity users. I know this question has been asked, but I cannot get any of the solutions to work for my application. I get a "compiler error: unable to start play mode" error every time I try so start the game. I am trying to get an enemy to move left and right in a loop, Here's what I have...any help appreciated..

using UnityEngine; using System.Collections;

public class Oscillate : MonoBehaviour { int maxValue = 25; int minValue = -25; int currentValue = 0; int direction = 1;

 void Update ()
 {
     currentValue += Time.deltaTime * direction;

     if (currentValue >= maxValue) {
         direction *= -1;
         currentValue = maxValue;
     } else if (currentValue <= minValue) {
         direction *= -1;
         currentValue = minValue;
     }

 }
     transform.position = new Vector3(currentValue, 0, 0);

}

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 areFranz · Sep 29, 2015 at 06:40 AM 0
Share

Try to use coroutines:

https://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines

http://docs.unity3d.com/$$anonymous$$anual/Coroutines.html

Something like:

void Start () { StartCoroutine( $$anonymous$$oveBackAndForth ()); }

IEnumerator $$anonymous$$oveBackAndForth () { // your code here // remember to use yield statement to wait for seconds between the Back and the Forth functions... }

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by landon912 · Sep 30, 2015 at 12:30 AM

Why not use a naturally oscillating function instead of manually hacking it?

 using UnityEngine;
 using System.Collections;
 
 public class OccilatingPosition : MonoBehaviour
 {
     public enum OccilationFuntion { Sine, Cosine }
 
     public void Start ()
     {
         //to start at zero
         StartCoroutine (Oscillate (OccilationFuntion.Sine, 1f));
         //to start at scalar value
         //StartCoroutine (Oscillate (OccilationFuntion.Cosine, 1f));
     }
 
     private IEnumerator Oscillate (OccilationFuntion method, float scalar)
     {
         while (true)
         {
             if (method == OccilationFuntion.Sine)
             {
                 transform.position = new Vector3 (Mathf.Sin (Time.time) * scalar, 0, 0);
             }
             else if (method == OccilationFuntion.Cosine)
             {
                 transform.position = new Vector3(Mathf.Cos(Time.time) * scalar, 0, 0);
             }
             yield return new WaitForEndOfFrame ();
         }
     }
 }

 
Comment
Add comment · Show 4 · 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 IansLJ · Sep 30, 2015 at 12:47 AM 0
Share

@landon91235

that is awesome. thank you so much for the help. It definitely works, I guess I just don't understand why. I am VERY new to this (in my 5th week of class) and I was having a lot of trouble with this. Even with the Unity tutorial videos, as well as many others, I still feel like I'm mainly just copying what other people do / have done rather than actually learning What I'm doing, if that makes sense. Hopefully I'll get it down with time.

Thanks to everyone here for their contributions!

avatar image landon912 IansLJ · Sep 30, 2015 at 01:06 AM 0
Share

Please convert this to a comment. Do you not understand the math behind it or the program$$anonymous$$g side? The first step would be to understand the math and logic behind it.

avatar image IansLJ landon912 · Sep 30, 2015 at 03:46 AM 0
Share

I can't say I fully understand either. I have been out of school for several years, and this was the first course my university offers for my intended I.T. major. Still all kinda looks like (insert foreign language here) to me, friend.

avatar image SpaceManDan IansLJ · Sep 30, 2015 at 03:13 AM 0
Share

@lansLJ

I know how you feel. I still feel that way sometimes but eventually you'll have a moment where you'll think of something you need to make and go. OH... I know how to do that.

avatar image
0

Answer by SpaceManDan · Sep 29, 2015 at 08:01 AM

I think your problem is that Time.deltaTime is a float and currentValue is an Int. You can not implicitly convert a float to an int. You need to change your int currentValue to a float?

Comment
Add comment · Show 1 · 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 SpaceManDan · Sep 29, 2015 at 08:05 AM 0
Share

Actually you'll need to convert almost all of those ints into floats if you plan to use deltaTime.

In case you don't know how, here are two examples.

 float currentValue;
 float direction = 1f;

and so on.

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

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

transform.position assign attempt for 'RCCMainCamera' is not valid. Input position is { NaN, NaN, NaN }. UnityEngine.Transform:set_position(Vector3) 1 Answer

Stop moving gameObject and push it back 0 Answers

how can I move an object around a parent object manually? 0 Answers

Turn Based Movement - Using Movement Points 0 Answers

Translating an Object to a Random Endpoint,Moving an Object to a random endpoint 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