Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by TRG96 · Aug 30, 2014 at 05:49 PM · mathf.sin

Using mathf.Sin() to move an object

I have an object which I want to move left and right and I am using the following code to do so

 xPos= Mathf.Sin(Time.time);
         transform.position = new Vector3(xPos, 0, 0);

This puts the object in the center of the world and it does move it left and right but I want it to stay in its current position and then move. If the transform.position.x is at 10, how can I move it so it goes to 11 and then to 9? I have tried

xPos += transform.position.x;

to get the current position but then the behavior completely changes.

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

3 Replies

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

Answer by BMayne · Aug 30, 2014 at 06:36 PM

Hey There,

Your object goes to the centre of the world because you set its position based on Sin [ a range of -1 to 1]. All you need to do is save an offset. Take a look at the code below.

 private Vector3 _startPosition;
 
 void Start () 
 {
     _startPosition = transform.position;
 }
 
 void Update()
 {
      transform.position = _startPosition + new Vector3(Mathf.Sin(Time.time), 0.0f, 0.0f);
 }

We save the objects start position so that it always moves from that point and not the centre of the world.

If you don't want to cache it's position you can do this

   _newPosition = transform.position;
   _newPosition .x += Mathf.Sin(Time.time) * Time.deltaTime;
   transform.position = _newPosition ;

The reason "the behaviour completely changes" when using += is because you are adding anywhere between -1 and 1 unites every frame (60 time per second). This will make your object shoot across the screen. You can use Delta Time as a scaler to control it's speed (it's also a good rule of thumb to use delta time on all movement code).

Regards,

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 Glenn-Korver · Apr 29, 2016 at 09:41 AM 0
Share
 _newPosition = transform.position;
    _newPosition .x += $$anonymous$$athf.Sin(Time.time) * Time.deltaTime;
    transform.position = _newPosition ;

This does not do the same as your previous example. you are constantly adding to pos.x with an increasing value, the end result wil not be that x moves a distance of 1 to left and right.

It will be a Sine movement but you have lost control of the magnitude at this point.

avatar image TooManySugar · Nov 20, 2016 at 07:40 PM 0
Share

Ins$$anonymous$$d of that vector3 shouldn´t be a float? you're just modifying the vector3.x

transform.position = _startPosition + new Vector3($$anonymous$$athf.Sin(Time.time), 0.0f, 0.0f);

avatar image
3

Answer by $$anonymous$$ · Sep 15, 2018 at 04:29 AM

 public float speedUpDown = 1;
 public float distanceUpDown = 1;
 
 void Update () 
 {
         Vector3 mov = new Vector3 (transform.position.x, Mathf.Sin(speedUpDown * Time.time) * distanceUpDown, transform.position.z);
         transform.position = mov;
     }

Comment
Add comment · 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
1

Answer by CSK_Sethu · May 22, 2019 at 02:18 PM

You can control the speed of oscillation by introducing a "time" variable that controls the time period i.e. the time taken to complete left and right motion:

 public float timePeriod = 2;
 public float height = 30f;

 private float timeSinceStart;

 private Vector3 pivot;

 private void Start()
 {
     pivot = transform.position;
     height /= 2;
     timeSinceStart = (3 * timePeriod) / 4;
 }

 void Update()
 {
     Vector3 nextPos = transform.position;
     nextPos.y = pivot.y + height + height * Mathf.Sin(((Mathf.PI * 2) / timePeriod) * timeSinceStart);

     timeSinceStart += Time.deltaTime;
     transform.position = nextPos;
 }

This is more flexible than using a "speed" variable

Plus, the code makes sure that the object always starts at the initial position. The reason timeSinceStart is used is to offset the time value so that the very first frame the Mathf.Sin() function evaluates to '-1', and the object starts at the initial position, instead of halfway between the beginning and the destination.

Feel free to ask me doubts or inform me about bugs :)

Comment
Add comment · 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

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

26 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

Related Questions

Convert Mathf.Sin() radians to degrees 2 Answers

How to make a float incrementally go up, reach the top, then go back down again and stop 1 Answer

Jump character using Sine Curve 1 Answer

Line drawing: How can I interpolate between points to create a smooth arc? 4 Answers

How does this AND Mathf.Sin actually work?! 2 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