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
0
Question by Karsnen_2 · Jun 13, 2012 at 05:51 PM · c#movementtransformmath

Implement the equation as a code?

Hello devs,

I am trying to create a two dimensional wave movement and I want to create in such a way that a gameobject moves along the x and z axis. Then once a gameobject reaches the target I want it to travel back with similar wave motion. I would like this to be performed as long as I wish. I played around lerp & movetowards, which performed well but they lacked a smoothness or a curve in their motion. I prefer the motion to be in 2 dimensional and not 3 dimensional. I happen to find this formula which might help but at the same time I wondered whether I could get help from you guys.

The Formula

If you guys tend to find a simpler solution, kindly let me know. I would highly appreciate it.

Thank you,

Karsnen.

Source : http://galileo.phys.virginia.edu/classes/152.mf1i.spring02/Waves2D_3D.htm

image002.gif (320 B)
Comment
Add comment · Show 1
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 Fattie · Jun 13, 2012 at 06:29 PM 0
Share

perhaps simply use an Animation. a whole animation system is built in to unity. they have good instructional videos.

i thin the formula is almost totally irrelevant. If yo do it with an equation and move it yourself, just use "sin". That will give you the simply wiggly motion you are looking for. Good luck

1 Reply

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

Answer by Scribe · Jun 13, 2012 at 06:37 PM

Hi, I don't know about using the formula you gave but if I've understood you could do it like this:

 var Speed = 1;
 var Amplitude = 1;
 var TargetPos : Vector3;
 
 function Update () {
     transform.position.y = Mathf.Sin(Vector3.Distance(transform.position, TargetPos)*Speed) * Amplitude;
 }

The target position could be an object with a transform component depending on your set-up but the same principal applies.

Hope thats useful!

Scribe

EDIT: try this instead, the last version took into account the distance in y which we weren't changing. This hopefully will be correct!

 var Speed = 1;
 var Amplitude = 1;
 var TargetPos : Vector3;
 
 function Update () {
     transform.position.y = Mathf.Sin((Mathf.Sqrt((TargetPos.x-transform.position.x)*(TargetPos.x-transform.position.x) + (TargetPos.z-transform.position.z)*(TargetPos.z-transform.position.z)))*Speed) * Amplitude;
 }

EDIT2 --------------------------------------------------------------------------------------------------------------------------------------- From your comment your best bet/ the easiest thing would be to go with fattie's suggestion of using an animation. However I was quite bored and like maths so I gave it a go, this is the result:

 var Speed = 1.0;
 var Amplitude = 1.0;
 var Waypoints : Vector3[];
 
 private var WaveLength : float;
 private var i = 0;
 private var TargetPos : Vector3;
 private var StartPos : Vector3;
 private var Line1;
 private var Grad1 : float;
 private var Grad2 : float;
 private var k1 : float;
 private var k2 : float;
 private var MaxDist : float;
 private var MedianPos : Vector3;
 
 function Start () {
     NextWaypoint();
 }
 
 function Update () {
     var MedianXPos : float;
     var MedianZPos : float;
     var CurDist : float;
     var Offset : Vector3;
     
     if(Grad1 == 0){
         MedianXPos = transform.position.x;
         MedianZPos = k1;
         CurDist = Vector3.Distance(Vector3(TargetPos.x, 0, TargetPos.z), Vector3(MedianXPos, 0, MedianZPos));
         Offset = Vector3(transform.position.x - MedianXPos, 0, transform.position.z - MedianZPos);
     }else if(Mathf.Abs(Grad1) == Mathf.Infinity){
         MedianXPos = StartPos.x;
         MedianZPos = transform.position.z;
         CurDist = Vector3.Distance(Vector3(TargetPos.x, 0, TargetPos.z), Vector3(MedianXPos, 0, MedianZPos));
         Offset = Vector3(transform.position.x - MedianXPos, 0, transform.position.z - MedianZPos);
     }else{
         k2 = transform.position.z - (Grad2 * transform.position.x);
         MedianXPos = (k1-k2)/(Grad2-Grad1);
         MedianZPos = Grad1*MedianXPos + k1;
         CurDist = Vector3.Distance(Vector3(TargetPos.x, 0, TargetPos.z), Vector3(MedianXPos, 0, MedianZPos));
         Offset = Vector3(transform.position.x - MedianXPos, 0, transform.position.z - MedianZPos);
     }
     
     MedianPos = Vector3(Mathf.MoveTowards(MedianXPos, TargetPos.x, Speed * Time.deltaTime), MedianPos.y, Mathf.MoveTowards(MedianZPos, TargetPos.z, Speed * Time.deltaTime));
     
     var SinOffset : Vector3;
     if(Grad1 == 0){
         SinOffset = Vector3(0, 0, Mathf.Sin(CurDist*WaveLength));
     }else if(Mathf.Abs(Grad1) == Mathf.Infinity){
         SinOffset = Vector3(Mathf.Sin(CurDist*WaveLength), 0, 0);
     }else{
         SinOffset = Vector3(Mathf.Sin(CurDist*WaveLength), 0, Grad2*Mathf.Sin(CurDist*WaveLength));
     }
     
     transform.position = Vector3(MedianPos.x + (SinOffset.x*Amplitude), transform.position.y, MedianPos.z + (SinOffset.z*Amplitude));
     
     Debug.DrawLine (Vector3(MedianXPos, 0, MedianZPos) + Offset, TargetPos+Offset, Color.red);
     
     if(TargetPos.x == transform.position.x && TargetPos.z == transform.position.z){
         if(i < (Waypoints.Length - 1)){
             i++;
             StartCoroutine("NextWaypoint");
         }else{
             Debug.Log("Journey finished");
         }
     }
 }
 
 function NextWaypoint () {
     TargetPos = Waypoints[i];
         
     StartPos = transform.position;
     MedianPos = transform.position;
     
     Line1 = Vector3(TargetPos.x - StartPos.x, 0, TargetPos.z - StartPos.z);
     Grad1 = Line1.z/Line1.x;
     k1 = StartPos.z - (Grad1 * StartPos.x);
     
     if(Grad1 == 0){
         Grad2 = (-1.0)/Grad1;
     }else if(Mathf.Abs(Grad1) == Mathf.Infinity){
         Grad2 = 0;
     }else{
         Grad2 = (-1.0)/Grad1;
     }
     k2 = StartPos.z - (Grad2 * StartPos.x);
     
     MaxDist = Vector3.Distance(Vector3(TargetPos.x, 0, TargetPos.z), Vector3(StartPos.x, 0, StartPos.z));
     WaveLength = (2 * Mathf.PI)/MaxDist;
     StopCoroutine("NextWaypoint");
 }

There are probably lots of better ways to do the same thing but this is what I came up with, it seems to work at least!

Scribe

Comment
Add comment · Show 6 · 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 Scribe · Jun 13, 2012 at 06:39 PM 0
Share

Speed is probably better described as WaveLength actually :)

avatar image Karsnen_2 · Jun 13, 2012 at 07:21 PM 0
Share

But then could you vary the movement (oscillation)?

While when my object moves from (-x,-z) to (x,z) through a sin wave, I would like to change the osicillation to (-x,z) to (x,z) or (x,-z).

avatar image Scribe · Jun 13, 2012 at 08:20 PM 0
Share

I think you've lost me! :D

Is this what you would like it to do?

avatar image Karsnen_2 · Jun 13, 2012 at 08:31 PM 0
Share

Yeah partially right.

But the difference in my view is,

  1. ins$$anonymous$$d of multiple waves - just one wave from the current position to target position.

  2. Upon reaching "target" I want to change the initial position.

Like this image. alt text

avatar image Scribe · Jun 14, 2012 at 04:53 PM 0
Share

check my second edit, it should work though your setup means you might be better off using an animation rather than doing this mathematically as I have :) Scribe

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to allow movement of game object to certain points only 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

Finding the axis of a curving pipe 0 Answers

No movement when animation plays (2d) 1 Answer


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