Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GrandpaRoji · Aug 21, 2017 at 09:34 AM · movingfloatingmathf.sin

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;
     }
 
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GrandpaRoji · Aug 21, 2017 at 03:32 PM 1
Share

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 !

avatar image christoph_r GrandpaRoji · Aug 21, 2017 at 04:43 PM 1
Share

Good to hear! Feel free to accept the answer so other people know this is solved.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

68 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to avoid enemy flocking 1 Answer

rotating/moving colliders with or without rigidbodies? 2 Answers

Help moving an object towards one it is looking at. 1 Answer

Dragging Objects with the mouse 1 Answer

Moving Platforms + Third Person Controller 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges