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);
}
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... }
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 ();
}
}
}
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!
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.
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.
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.
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?
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.