Search Unity

roll a ball with android

Discussion in 'Editor & General Support' started by oldcollins, Jun 2, 2013.

  1. oldcollins

    oldcollins

    Joined:
    Aug 3, 2012
    Posts:
    111
    i got this script for a roll a ball built forandroid device it works fine but it's all back wards so some help would be welcome

    here's the script

    Code (csharp):
    1. var tilt : Vector3 = Vector3.zero;
    2. var speed : float;
    3. private var circ : float;
    4. private var previousPosition : Vector3;
    5.  
    6. @script RequireComponent(Rigidbody)
    7. function Start()
    8. {
    9.     //Find the circumference of the circle so that the circle can be rotated the appropriate amount when rolling
    10.     circ = 2 * Mathf.PI * collider.bounds.extents.x;   
    11.     previousPosition = transform.position;
    12. }
    13.  
    14.  
    15. function Update () {
    16.     tilt.x = -Input.acceleration.y;
    17.     tilt.z = Input.acceleration.x;
    18.    
    19.     rigidbody.AddForce(tilt * speed * Time.deltaTime);
    20. }
    21.  
    22. function LateUpdate()
    23. {
    24.     var movement : Vector3 = transform.position - previousPosition;
    25.    
    26.     movement = Vector3(movement.z,0,  -movement.x);
    27.    
    28.     transform.Rotate(movement / circ * 360, Space.World);
    29.    
    30.     previousPosition = transform.position; 
    31.    
    32. }
     
  2. Sir-Tiddlesworth

    Sir-Tiddlesworth

    Joined:
    Oct 19, 2011
    Posts:
    908
    This maybe?

    Code (csharp):
    1. var tilt : Vector3 = Vector3.zero;
    2. var speed : float;
    3. private var circ : float;
    4. private var previousPosition : Vector3;
    5.  
    6. @script RequireComponent(Rigidbody)
    7.  
    8. function Start(){
    9.  
    10.     //Find the circumference of the circle so that the circle can be rotated the appropriate amount when rollin
    11.     circ = 2 * Mathf.PI * collider.bounds.extents.x;    
    12.     previousPosition = transform.position;
    13. }
    14.  
    15. function Update () {
    16.  
    17.     tilt.x = Input.acceleration.y;
    18.     tilt.z = -Input.acceleration.x;
    19.  
    20.     rigidbody.AddForce(tilt * speed * Time.deltaTime);
    21. }
    22.  
    23. function LateUpdate(){
    24.  
    25.     var movement : Vector3 = transform.position - previousPosition;
    26.    
    27.     movement = Vector3(movement.z,0,  -movement.x);
    28.     transform.Rotate(movement / circ * 360, Space.World);
    29.     previousPosition = transform.position;  
    30. }
     
    carolsusieo likes this.
  3. oldcollins

    oldcollins

    Joined:
    Aug 3, 2012
    Posts:
    111
    i got it working now but id like it to work so the phone face's you not holding the phone horizontal
     
  4. dare00

    dare00

    Joined:
    Jun 30, 2014
    Posts:
    1
    then wouldn't you just change your axes?
     
  5. mbtamuli

    mbtamuli

    Joined:
    Nov 16, 2014
    Posts:
    1
    Can this be added along with the keyboard controls?
    Like I already have this code as shown in the tutorial.

    Code (CSharp):
    1. void FixedUpdate()
    2. {
    3.     float moveHorizontal = Input.GetAxis ("Horizontal");
    4.     float moveVertical = Input.GetAxis ("Vertical");
    5.  
    6.     Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    7.     rb.AddForce (movement * speed);
    8. }
    Can I just add this code and it will work for both keyboard input and accelerometer input?
    Code (CSharp):
    1. tilt.x = Input.acceleration.y;
    2. tilt.z = -Input.acceleration.x;
    3. rigidbody.AddForce(tilt * speed * Time.deltaTime);