Mathf.pingpong (64098)

when i use mathf.pingpong it move my object in both directions however i want it to start at 0 and go to ‘n’, as it stands instead of moving in both directions. My object is a simple cube object.

here is the code i am using to do this

using UnityEngine;
using System.Collections;

public class movePointer : MonoBehaviour {
// Use this for initialization
void Start () {

}
// Update is called once per frame
void Update () {
	transform.localScale = new Vector3(0.1f,0.1f,1.0f*Mathf.PingPong(Time.time*5.0f, 4));
	Debug.Log(transform.localScale = new Vector3(0.1f,0.1f,1.0f*Mathf.PingPong(Time.time*5.0f, 4)));
}
}

I know I havent explained what I want well so pls see the below image for a clearer explanation

current both ends move
i only want one end to move

Any help is appreciated

4 Answers

4

To get the behavior you want, remove the script from your block and attach it to an empty game object. Move the empty game object to the end of the block then make the block a child of the empty game object.

I've searched my dropbox and i've found it again (i knew i've drawn one): <img src="https://dl.dropbox.com/u/7761356/UnityAnswers/Images/EmptyGOExample.PNG">

Sorry dis didnt work for me, the block just disappeared on me so now i cant see this. thanks for the help though

The code example at Unity - Scripting API: Mathf.PingPong does exactly what you want.

@Graham - he says "moving" but in the code he is scaling, and he is wondering how to make it scale in only one direction.

I assumed that scaling the object was a mistake. He uses non-uniform scale, which typically causes problems. But yeah, his diagrams show scaling.

scaling is exactly what i meant sorry for confusion

You can’t scale an object from one end. What you will need to do is scale and translate the object at the same time. That will give you the look you are going for.

how would i do this, is it with transform.translate and transform .localScale

Yes. Alternatively you could just physically change the transform.position of the item as well. I personally would Mathf.Lerp the equation to make sure it fit without having to do complicated math. Use the start point and scale as 0 for lerps and then the 1 point as the furthest point. Then you can use the two lerps to make sure it looks the way you want. I'm not on my unity comp atm but if needed I can mock up some keep code for ya.

as i couldnt get this going, an easy solution was sticking with the same code i had at the beginning.

Create an object in 3ds max and move it pivot point to one end, export it as an fbx file and import into unity.

Attach the script to this object and it works.

However i dont have a coded answer.

Other free 3d software will also work to do this if you cant get 3ds max