How do I change the speed of this script
Hi. I have this script that moves an object in a straight line. When you insert the delay factor, the lower the number, the faster it goes. But it does not use fractions. This means that if I want it to go faster than 1, logically I should put in 0.5 or 0.6 - something like that. But then it just doesn't move. How can I get the object to travel faster? Heres the script
using UnityEngine; using System.Collections;
public class MoveAudioObject : MonoBehaviour {
// Use this for initialization
public float axisStart,axisEnd;
public float delayFactor;
public int axis; //1 =x, 2=y,3=z
private int speedFactor,direction;
void Start () {
speedFactor = 0;
direction = 1;
}
// Update is called once per frame
void Update () {
speedFactor++;
if (speedFactor == delayFactor) {
speedFactor = 0;
switch(axis)
{
case 1:
if (direction == 1) {
transform.position -= new Vector3 (0.1f, 0, 0);
if (transform.position.x <= axisEnd)
direction = 2;
} else if (direction == 2) {
transform.position += new Vector3 (0.1f, 0, 0);
if (transform.position.x >= axisStart)
direction = 1;
}
break;
case 2:
if (direction == 1) {
transform.position -= new Vector3 (0, 0.1f, 0);
if (transform.position.y <= axisEnd)
direction = 2;
} else if (direction == 2) {
transform.position += new Vector3 (0, 0.1f, 0);
if (transform.position.y >= axisStart)
direction = 1;
}
break;
case 3:
if (direction == 1) {
transform.position -= new Vector3 (0, 0, 0.1f);
if (transform.position.z <= axisEnd)
direction = 2;
} else if (direction == 2) {
transform.position += new Vector3 (0, 0, 0.1f);
if (transform.position.z >= axisStart)
direction = 1;
}
break;
}
/*if (transform.position.z <= zEnd) {
Debug.Log ("++");
transform.position += new Vector3 (0, 0, 1);
} else if (transform.position.z >= zStart) {
Debug.Log ("--");
transform.position -= new Vector3 (0, 0, 1);
}*/
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612123008im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Vector3 magnitude broken? 1 Answer
How to revert speed back to normal after changing with Key. 1 Answer
Something is wrong with my max. speed code 0 Answers
drag AND drop push and pull object 0 Answers