Urgent help needed on Mathf.PingPong

This is my code of line

transform.position = new Vector3(-6.6f + Mathf.PingPong(Time.time*60, 8.6f+0.57f), transform.position.y, transform.position.z);

My gameobject is moving between the limits, but i want it to be on just 2 values and not between those two values.

My numbers are -6.6f and 0.57f and not between these 2 values.

Please please help me.

bump

What do you mean, do you want the object to teleport between those 2 positions?

yes teleport with speed Time.time*60

again and again

You want it to change back and forth 60 times a second?

Maybe he wants it to teleport, wait 60 seconds, and teleport back?

Could be.

Add this class to your project. It’s in javascript but it should be trivial to convert to C# if you need. This isn’t the only (or even the bestest) way to do it but it should get the job done.

Use it by starting a timer sequence by creating a variable in start / awake or where ever you start the thing ping ponging.

var myToggle = new ToggleBackForth(-6.6f, 0.57f, 1.0f);

In your update code your transforms x value would then be myToggle.GetCurrent()

transform.position = new Vector3(myToggle.GetCurrent(), transform.position.y, transform.position.z);

When your done toggling back and forth call

myToggle.StopTimer();

ToggleBackForth.js

//toggle between start value and end value given a duration
class ToggleBackForth
{
private var done : boolean;
private var current : float ;
private var stopat : float;
private var buffer : float;
private var toggleTime : float;

//default constructor if using this you need to manually start the timer using StartTimer()
function ToggleBackForth()
	{
	done = false;
	current = -6.6f;
	stopat = 0.57f;
	buffer = current;
	toggleTime = 5.0;
	}

function ToggleBackForth(start : float, end : float, duration : float)
	{
	done = false;
	current = start;
	stopat = end ;
	buffer = current;
	toggleTime = duration;
	StartTimer();
	}

function StartTimer()
	{
	while(!done )
		{
		buffer = current;
		current = stopat;
		stopat = buffer;
		yield WaitForSeconds (toggleTime);
		}
	}

function StopTimer()
	{
	done = true;
	}

function GetCurrent():float
	{
	return current;
	}
}

i can change it twice per second, and can you help me with cs, i dont know js

i tried the js scipt too, it says

The associated script cant be loaded. Please fix any compile errors and assign a valid script.

But there is no error in the console…

This is a special case scenario in Unity when C# scripts attempt to access Javascripts. C# scripts compile first and won’t know about javascripts.
I’ve shamelessly copied this from here : http://unitygems.com/mistakes1/

i simply want my game Object to transform to 2 positions 2,3 times every second

transform.position = new Vector3(-6.6f, transform.position.y, transform.position.z);
transform.position = new Vector3(1.0f, transform.position.y, transform.position.z);

I hacked this one together for you.

SomeBehaviourScript.cs <-goes on the object that will move

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour 
{
	ToggleBackForth myToggle;
	// setup the toggle to make a move ever 1.0 seconds
	void Start () 
	{
	myToggle = new ToggleBackForth(-6.6f, 0.57f, 1.0f);
	StartCoroutine(myToggle.StartTimer());
	}
	
	// Update is called once per frame
	void Update () 
	{
	transform.position = new Vector3(myToggle.GetCurrent(), transform.position.y, transform.position.z);
	}
}

Your new Toggle function in C#:

ToggleBackForth.cs

using UnityEngine;
using System.Collections;

public class ToggleBackForth
    {
    public bool done;
    public float current;
    public float stopat;
    public float buffer;
    public float toggleTime;
     
    //default constructor if using this you need to manually start the timer using StartTimer()
    public ToggleBackForth()
        {
        done = false;
        current = -6.6f;
        stopat = 0.57f;
        buffer = current;
        toggleTime = 5.0f;
        }
     
    public ToggleBackForth(float start, float end, float duration)
        {
        done = false;
        current = start;
        stopat = end ;
        buffer = current;
        toggleTime = duration;
        }
     
    public IEnumerator StartTimer()
        {		
        while(!done)
            {
            buffer = current;
            current = stopat;
            stopat = buffer;
            yield return new WaitForSeconds(toggleTime);	
			}		
        }

     
    public void StopTimer()
        {
        done = true;
        }
     
    public float GetCurrent()
        {
        return current;
        }
    }