C# Unity3D Scale using Accelerometer with Clamp

user2360140

I'm trying to translate the roll(y) of a phone into the scale of an object but with size limits.

I have had success scaling to infinity +- with the following code:

void Update () {
   float accelY = Input.acceleration.y;
   transform.localScale += new Vector3(0, accelY, 0);
}

The problem I am having is with the max and min limits of the scale.

I attempted to use Clamp in the following way but have ended up just limiting the direction of scale where -y (rolling toward myself) equals 0 therefore no shrinking happens and +y (rolling away from self) scales up endlessly assumed at a max rate of 0.045f.

transform.localScale += new Vector3(0, Mathf.Clamp(accelY, 0f, 0.045f), 0);

In an ideal world I'd like to have a min scale of y = 1 and a max scale y = 100 and as you roll the phone back and forth it would tween between the two sizes.

I can find code snippets for Translate and rotate but nothing for scaling.. Please help.

kamyker
void Update () {
   float accelY = Input.acceleration.y;
   transform.localScale = new Vector3(0, Mathf.Clamp(transform.localScale.y + accelY, 1f, 
   100f), 0)
}

You should add Time.deltaTime because Update is frames/sec dependant:

[SerializeField]
float speed = 10f;
[SerializeField]
float minSize = 1f;
[SerializeField]
float maxSize = 100f;

void Update () {
   float accelY = Input.acceleration.y * Time.deltaTime * speed;
   transform.localScale = new Vector3(0, Mathf.Clamp(transform.localScale.y + accelY, minSize , 
   maxSize ), 0)
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

unity3d - Accelerometer sensitivity

From Dev

Unity3d using CreateSpecificCulture

From Dev

how to download a file from url and save in location using unity3d in C sharp?

From Dev

Using Android accelerometer y axis falls through terrain - Unity3D

From Dev

How turn On/Off android flashlight using C# only in Unity3d

From Dev

Using Mathf.Clamp in unity for boundries

From Dev

: (colon) in c# for Unity3d

From Dev

Using a Singleton in Unity3D

From Dev

Unity 3D realistic accelerometer control

From Dev

How to get variables from AndroidJavaObject into a C# class using Unity3D

From Dev

How can I detect a shake motion on a mobile device using Unity3D? C#

From Dev

Using a List<> to store methods in C# Unity3D?

From Dev

c# Unity3D Using toggle button to change boolean in another script and return a sum

From Dev

Unity3D: Efficiency of using a queue(list) of IEnumerators, C#?

From Dev

Unity3D C#. Using the transform.RotateAround() function while keep the distance between two objects constant

From Dev

unity3d - Accelerometer sensitivity

From Dev

Unity3d plugin is written in c#, I'd like to interact with it using UnityScript

From Dev

Unity3d using CreateSpecificCulture

From Dev

How turn On/Off android flashlight using C# only in Unity3d

From Dev

NullReferenceException in Unity3d using unityscript

From Dev

What PRNG is Unity3D using?

From Dev

How to make a Unity3D game just using C#

From Dev

How to get variables from AndroidJavaObject into a C# class using Unity3D

From Dev

Cannot convert data from textfile to array using .Split (C# Unity3D)

From Dev

C# Clamp object to window?

From Dev

How to clamp camera in Unity3D

From Dev

How to get model description list using c# in unity3d?

From Dev

Unity - How To Disable Accelerometer?

From Dev

(C++) Clamp 2D Position Within Circle (Drawn Using Midpoint Circle Algorithm)

Related Related

  1. 1

    unity3d - Accelerometer sensitivity

  2. 2

    Unity3d using CreateSpecificCulture

  3. 3

    how to download a file from url and save in location using unity3d in C sharp?

  4. 4

    Using Android accelerometer y axis falls through terrain - Unity3D

  5. 5

    How turn On/Off android flashlight using C# only in Unity3d

  6. 6

    Using Mathf.Clamp in unity for boundries

  7. 7

    : (colon) in c# for Unity3d

  8. 8

    Using a Singleton in Unity3D

  9. 9

    Unity 3D realistic accelerometer control

  10. 10

    How to get variables from AndroidJavaObject into a C# class using Unity3D

  11. 11

    How can I detect a shake motion on a mobile device using Unity3D? C#

  12. 12

    Using a List<> to store methods in C# Unity3D?

  13. 13

    c# Unity3D Using toggle button to change boolean in another script and return a sum

  14. 14

    Unity3D: Efficiency of using a queue(list) of IEnumerators, C#?

  15. 15

    Unity3D C#. Using the transform.RotateAround() function while keep the distance between two objects constant

  16. 16

    unity3d - Accelerometer sensitivity

  17. 17

    Unity3d plugin is written in c#, I'd like to interact with it using UnityScript

  18. 18

    Unity3d using CreateSpecificCulture

  19. 19

    How turn On/Off android flashlight using C# only in Unity3d

  20. 20

    NullReferenceException in Unity3d using unityscript

  21. 21

    What PRNG is Unity3D using?

  22. 22

    How to make a Unity3D game just using C#

  23. 23

    How to get variables from AndroidJavaObject into a C# class using Unity3D

  24. 24

    Cannot convert data from textfile to array using .Split (C# Unity3D)

  25. 25

    C# Clamp object to window?

  26. 26

    How to clamp camera in Unity3D

  27. 27

    How to get model description list using c# in unity3d?

  28. 28

    Unity - How To Disable Accelerometer?

  29. 29

    (C++) Clamp 2D Position Within Circle (Drawn Using Midpoint Circle Algorithm)

HotTag

Archive