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


    May 29, 2009
    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


    May 29, 2009
    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;
    5. var sensitivity : float = 10.0;
    7. var forwardForce : float = 1.0;
    8. var guiSpeedElement : Transform;
    10. private var normalizedSpeed : float = 0.2;
    11. private var euler : Vector3 =;
    13. var horizontalOrientation : boolean = true;
    15. function Awake () {
    16.     if (horizontalOrientation)
    17.         Screen.SetResolution(480, 320, true);
    18.     else
    19.         Screen.SetResolution(320, 480, true);
    21.     guiSpeedElement.position = new Vector3 (0, normalizedSpeed, 0);
    22. }
    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. }
    36. function FixedUpdate () {
    37.     rigidbody.AddRelativeForce(0, 0, normalizedSpeed * forwardForce);
    39.     var accelerator : Vector3 = iPhoneInput.acceleration;
    41.     if (horizontalOrientation)
    42.     {
    43.         var t : float = accelerator.x;
    44.         accelerator.x = -accelerator.y;
    45.         accelerator.y = t;
    46.     }
    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);
    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);
    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


    Unity Product Evangelist

    Dec 8, 2006
    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


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

    Lazy bastard.

  5. HiggyB


    Unity Product Evangelist

    Dec 8, 2006
    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


    May 29, 2009
    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


    Unity Product Evangelist

    Dec 8, 2006
    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.