- Home /
Mathf.Sin used to move object left and right but when value changed in game it teleports around
So i'm creating a game where a ball floats left to right and when screen is clicked the ball stops moving and the objects around the ball move to simulate it moving up, but when it passes those objects it should continue to move, and increase in speed, which it does but the problem is that when it goes back to moving it will sometimes teleport to the opposite side of the screen.... What is causing this and how can I fix it?
Here's the code for the ball:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour {
public static Ball instance;
float originalX;
bool isDead = false; //Bool if game is over (ball died)
bool holding = false; //Bool to see if ball is "moving" up or not and stops it from moving side to side
public float floatStrength = 1f; // change the range of x positions that are possible.
public float floatTime = 1.1f; // change how fast the ball moves side to side
float floatTimeBefore;
float startTimer = .1f; //fixes a bug where on restart it would already be in GameControl.instance.BallJump();
float currTime;
bool canClick = false;
void Awake()
{
//If we don't currently have a Ball
if (instance == null)
//set this one to be it
instance = this;
//otherwise
else if (instance != this)
//destroy this one because it is a duplicate.
Destroy(gameObject);
}
void Start()
{
//This chooses a random color
//GetComponent<SpriteRenderer>().color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
this.originalX = this.transform.position.x;
}
void FixedUpdate()
{
currTime += Time.deltaTime;
if (currTime >= startTimer)
{
canClick = true;
}
if (isDead == false)
{
if (Input.GetMouseButtonDown(0) && canClick == true )
{
GameControl.instance.BallJump();
holding = true;
floatTimeBefore = floatTime;
floatTime = 0;
}
if (holding == false)
{
transform.position = new Vector3(originalX + ((float)Mathf.Sin(Time.time * floatTime) * floatStrength),
transform.position.y,
transform.position.z);
}
}
}
//Checks to see if ball dies
void OnCollisionEnter2D(Collision2D other)
{
floatStrength = 0;
isDead = true;
GameControl.instance.BallDied();
}
//After point scores return the ball to normal floating and increase speed at which it moves
public void returnNormal()
{
floatTime = floatTimeBefore * 1.12f;
holding = false;
}
}
Answer by christoph_r · Aug 21, 2017 at 12:45 PM
It seems you're using Time.time, which keeps on running, even when your object is stopping. Try to use a variable like your currTime
that only keeps adding up when your object is moving.
Yeah that seemed to be causing a problem I added in a counter for when it was moving and when player clicks I just divide the currTime by what i'm increasing the floatTime by and it runs as smooth as can be! Thanks a lot @christoph_r !
Good to hear! Feel free to accept the answer so other people know this is solved.
Your answer
![](https://koobas.hobune.stream/wayback/20220612133208im_/https://answers.unity.com/themes/thub/images/avi.jpg)