Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Star Trooper for PC?

Discussion in 'Editor & General Support' started by Armagon, Jun 13, 2009.

  1. Armagon

    Armagon

    Joined:
    May 29, 2009
    Posts:
    246
    I'm trying to convert the iphone example StarTrooper to PC, with no luck. Any ideas on how to do it, guys? What needs to be fixed? When i try to play, Unity simply crashes. :x
     
  2. Armagon

    Armagon

    Joined:
    May 29, 2009
    Posts:
    246
    Finally got it working without crashing. Just need someone able to convert the player control to PC, instead of Iphone.

    Code (csharp):
    1. var turnSpeed : float = 10.0;
    2. var maxTurnLean : float = 50.0;
    3. var maxTilt : float = 50.0;
    4.  
    5. var sensitivity : float = 10.0;
    6.  
    7. var forwardForce : float = 1.0;
    8. var guiSpeedElement : Transform;
    9.  
    10. private var normalizedSpeed : float = 0.2;
    11. private var euler : Vector3 = Vector3.zero;
    12.  
    13. var horizontalOrientation : boolean = true;
    14.  
    15. function Awake () {
    16.     if (horizontalOrientation)
    17.         Screen.SetResolution(480, 320, true);
    18.     else
    19.         Screen.SetResolution(320, 480, true);
    20.  
    21.     guiSpeedElement.position = new Vector3 (0, normalizedSpeed, 0);
    22. }
    23.  
    24. function Update () {
    25.     for (var evt : iPhoneTouch in iPhoneInput.touches)
    26.     {
    27.         if (evt.phase == iPhoneTouchPhase.Moved)
    28.         {
    29.             normalizedSpeed = evt.position.y / Screen.height;
    30.             guiSpeedElement.position = new Vector3 (0, normalizedSpeed, 0);
    31.         }
    32.     }
    33. }
    34.  
    35.  
    36. function FixedUpdate () {
    37.     rigidbody.AddRelativeForce(0, 0, normalizedSpeed * forwardForce);
    38.    
    39.     var accelerator : Vector3 = iPhoneInput.acceleration;
    40.  
    41.     if (horizontalOrientation)
    42.     {
    43.         var t : float = accelerator.x;
    44.         accelerator.x = -accelerator.y;
    45.         accelerator.y = t;
    46.     }
    47.    
    48.     // Rotate turn based on acceleration       
    49.     euler.y += accelerator.x * turnSpeed;
    50.     // Since we set absolute lean position, do some extra smoothing on it
    51.     euler.z = Mathf.Lerp(euler.z, -accelerator.x * maxTurnLean, 0.2);
    52.  
    53.     // Since we set absolute lean position, do some extra smoothing on it
    54.     euler.x = Mathf.Lerp(euler.x, accelerator.y * maxTilt, 0.2);
    55.    
    56.     // Apply rotation and apply some smoothing
    57.     var rot : Quaternion = Quaternion.Euler(euler);
    58.     transform.rotation = Quaternion.Lerp (transform.rotation, rot, sensitivity);
    59. }
    Anyone? :roll:
     
  3. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    What exactly do you need help with and why are you rolling eyes at people? To me, the eye roll emoticon is a sign of contempt and not conducive to receiving help. ;)

    Instead of acceleration to control tilt look at using either the arrow keys or the mouse for that, look at the first person controller for some example scripts (mouse look for example). Or was there more needed? Or am I missing it altogether? :)
     
  4. FeareD

    FeareD

    Joined:
    Jun 12, 2009
    Posts:
    22
    I think your missing it, he wants someone to do the work for him.

    Lazy bastard.

    ::FeareD
     
  5. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Luckily I've chatted with him on IRC and based on my first impressions I don't think that's quite his angle. We'll see where he's stuck and offer guidance from there. :)
     
  6. Armagon

    Armagon

    Joined:
    May 29, 2009
    Posts:
    246
    That's really rude and uncalled for, not the behavior that should be expected on community forums, specially on forums of such a professional tool like Unity. I'll choose to ignore your comment on this thread.

    HiggyB, something like that. I tried to re-create the code using the code from the third person controller (the 3d platformer script) and first person controller as guides, but it's difficult because i simply don't know why the iPhone controller code uses a FOR statement:

    Code (csharp):
    1.  for (var evt : iPhoneTouch in iPhoneInput.touches)
    It's also difficult because i don't understand what evt.position.y is and why is it divided by the Screen.height. I need help on rewriting the code, since i don't understand the logic of the original one.

    Also, what's iPhoneInput.acceleration?
     
  7. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Good on ya, rise above and move on. :)


    When developing for the iPhone and iPod touch you access device input (touch, acceleration, orientation). In particular, the touches property is an array of touch events that happened in the last frame. Therefore if you have one finger on the screen then there is one entry in the array, on up to a maximum of five touches (device limit). That for loop is simply doing an iteration through all the touches found that frame to process them as needed.


    As you might surmise, the location of each touch on the screen is important and so the touch event (evt in this case) has a position property with individual components for the on-screen location. The y-value is being divided by the screen's height to work with a unitized position value (so it runs from 0.0 to 1.0).


    As noted above there are three primary input mechanisms on the iPhone/iPod touch: touch, acceleration and orientation. Touch is obvious, you touch with up to five fingers for zoom, swipe, button clicks, whatever. Orientation should be relatively obvious, is the device face up, face down, other? The acceleration is a value provided by an internal accelerometer so you can do some motion detection (or replicate the orientation data using the acceleration values).

    For example, my phone, just sitting here on my desk, has a "downward" acceleration due to gravity. If I (hold tightly and) flick my phone, it will jump to some other value along the x/y/z axes and folks may want to use that data in various interesting ways. :)


    Makin' sense? Post up and follow on questions and we'll keep at it.