Marlon Cassar
Marlon Cassar

Reputation: 11

Moving objects on y axis without input

Hi I am using this code to have objects moving on the y axis.

using UnityEngine;
using System.Collections;

public class TargetMovementVertical : MonoBehaviour 
{
    public int maxSpeed;

    private Vector3 startPosition;

    // Use this for initialization
    void Start () 
    {
        startPosition = transform.position;
    }

    // Update is called once per frame
    void Update ()
    {
        MoveVertical ();
    }

    void MoveVertical()
    {
        transform.position = new Vector3(transform.position.x, Mathf.Sin(Time.time * maxSpeed), transform.position.z);

        if(transform.position.y > 1.0f)
        {
            transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
        }
        else if(transform.position.y < -1.0f)
        {
            transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
        }
    }
}

My only problem is that the object are only moving within 1 and -1 and i would like to have them move lower. is there a possible way please ?

Upvotes: 1

Views: 489

Answers (1)

Theraot
Theraot

Reputation: 40325

Programming

In your code, you are setting the position using this line:

transform.position = new Vector3(transform.position.x, Mathf.Sin(Time.time * maxSpeed), transform.position.z);

Here, the only coordinate that is changing is the y coordinate. And it changes according to the function Mathf.Sin.

If you read the documentation for Mathf.Sin you will find that it returns values between -1 and +1.

That is why...

the object are only moving within 1 and -1

The simple solution is to multiply the result of Mathf.Sin by some factor.


Math

This is the sine function:

sine function plot

red plot: y = sin(x)

As you can see, the range of the sine function is [-1, 1]. Thus, regardless of what input value you put into the function, you will get a result in the interval [-1, 1].

If you multiply the input, you are changing the frequency of the sine wave, for example:

sine function plot

Red plot: y = sin(5x)

Observe that placing a factor inside the function will not affect the amplitud of the wave. Compare with the following:

sine function plot

Red plot: y = 5sin(x)

The above graph, at difference with the prior ones, has the range [-5, 5].

Here you can see them all for comparison:

sine function plots

Red plot: 5sin(x)

Blue plot: sin(x)

Purple plot: sin(5x)

These plots were created with the graphing calculator from meta-calculator. You can try the functions there yourself if you don't want to take my word for it.


To understand why the sin function has this shape, remember that the sine function takes an angle and returns the vertical component of a unit vector that has angle with the horizontal...

I mean this:

Unit circle with sine and cosine

Unit circle with sine and cosine, θ=45 degrees.

Since we are taking a unit vector, (we are working on the unit circle), the maximum value that the vertical (sine) will take is 1, and the minimum is -1.

To understand how the sine plots we saw above come from this, I hope this animation makes it clearer:

sine and cosine animation

Animation showing how the sine function (in red) y = sin(θ) is graphed from the y-coordinate (red dot) of a point on the unit circle (in green) at an angle of θ in radians.


Back to programming

As I said at the start of the answer, if you want to scale the movement, you can change the amplitude to the sine wave by multipliying the result by some factor, for example: Mathf.Sin(angle) * amplitude.

That amplitude value will tell how far the value will reach, that is, by multiplying by Mathf.Sin by amplitude you get a value in the range - amplitude and + amplitude.


I expect that you find that approach reasonable know that the reasoning behind it have been presented.

I hope the above explanation makes it clear that the sine function does not preserve factors. That is: sin(a*x) ≠ a*sin(x). In other words that the sine function is not transitive with scaling, the reason for that is that the sine function is NOT a linear transformation.

Upvotes: 3

Related Questions