Search Unity

Unity - Moving Camera and Sprint Boost Scripting help

Discussion in 'Scripting' started by ArminPasalic, Jun 18, 2011.

  1. ArminPasalic

    ArminPasalic

    Joined:
    Feb 8, 2011
    Posts:
    58
    Hello, I have created this simple ball game where you have to complete puzzles and stuff. By that, maybe moving a cube or something? I already got a Script for MOVING, PUSHING, DEATH.


    CAMERA PROBLEM:

    I would like to get some help on some scripting how to actually move the camera. As the player is a Sphere, I got SmoothFollow Script on the Camera to actually have the camera follow the player in third-person.

    As I got this problem that camera is only looking Front, if the Player moves against the Camera, the camera doesnt swich position. As know in MMO games, you can use Mouse2 to optional move the camera wherever you want, how can I create such script as well as having SmoothFollow script on my Camera?


    SPRINT BOOST:

    I would also like to get some help how to get a Fast Sprint Boost by clicking LeftShift. As I got a script for pushing things with Rigid Body, how can I make that the Sphere(player) get a fast boost, like if he is moving a cube then the cube will be moved a little bit more cause the Player used the Boost. Also, an Cooldown so its balanced(in Secs)


    I would be much glad if you could provide a script for that(Java or whatever) thanks! :)

    Regards,
    ArminP.
     
  2. Todilo

    Todilo

    Joined:
    Feb 1, 2011
    Posts:
    88
    For the cameraproblem I would suggest that you measure the speed of the ball in relatino to the camear position. If it is positive you do nothing, if it is negative you flip the positions. I am not familiar with the smoothfollow-camera script so I cant tell you the specifics but that is my thoughts.

    Sprintbutton: If it is an instance boost you want. like a thrust you could use rigidbody.addexplosionforce:
    http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddExplosionForce.html

    then start a cooldown timer which you in the update function decrease by Time.deltaTime each update. When it is below 0 you should be allowed to press left-shift again.
     
  3. ArminPasalic

    ArminPasalic

    Joined:
    Feb 8, 2011
    Posts:
    58
    Thanks for the Sprint Script, but it wasnt that I were looking for. But anyways thanks. Let me provide more info as I didnt inform it very well.

    I just could use some to get a Script for Sprinting on "LEFT SHIFT" or whatever button. It will give the Sphere(player) a small boost so he can move some radius forward or back faster, but if you use this Sprint Boost and hit a Rigid Body Cube it will increase the power of pushing it.

    And also, Cooldown as it doesnt need to be used every 2 sec.
     
  4. Todilo

    Todilo

    Joined:
    Feb 1, 2011
    Posts:
    88
    Is it the rotation you want to be increased then? Also for the collission you can setup a manua collission. So you flag a player to be boosted and then transfer that to the collided object giving it a push(can be interpolated over time so the push decreased of time).
     
  5. ArminPasalic

    ArminPasalic

    Joined:
    Feb 8, 2011
    Posts:
    58
  6. ArminPasalic

    ArminPasalic

    Joined:
    Feb 8, 2011
    Posts:
    58
    I just need a quick boost when I click Left Shift, like so the ball just moves faster and stops again, just like in Metroid Prime Game. But I also want a Cooldown so it cant be abused/spammed by the Player.
     
  7. Todilo

    Todilo

    Joined:
    Feb 1, 2011
    Posts:
    88
    Perhaps if you show us your script for moving the ball. My idea with the added force should work. It would give a short burst and nothing more but it of cause depends on your implementation of the movement.
     
  8. ArminPasalic

    ArminPasalic

    Joined:
    Feb 8, 2011
    Posts:
    58
    /// This script moves the character controller forward
    /// and sideways based on the arrow keys.
    /// It also jumps when pressing space.
    /// Make sure to attach a character controller to the same game object.
    /// It is recommended that you make only one call to Move or SimpleMove per frame.

    var speed : float = 6.0;
    var jumpSpeed : float = 8.0;
    var gravity : float = 20.0;
    private var dead = false;

    function OnControllerColliderHit(hit:ControllerColliderHit)
    {
    if(hit.gameObject.tag == "fallout")
    {
    dead = true;
    }
    }

    private var moveDirection : Vector3 = Vector3.zero;

    function Update() {
    var controller : CharacterController = GetComponent(CharacterController);
    if (controller.isGrounded) {
    // We are grounded, so recalculate
    // move direction directly from axes
    moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
    Input.GetAxis("Vertical"));
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= speed;

    if (Input.GetButton ("Jump")) {
    moveDirection.y = jumpSpeed;
    }
    }

    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;

    // Move the controller
    controller.Move(moveDirection * Time.deltaTime);
    }

    function LateUpdate()
    {
    if(dead)
    {
    transform.position = Vector3(38,4,11);
    gameObject.Find("Main Camera").transform.position = Vector3(38,4,-11);
    dead = false;
    }
    }



    -----------------------------------------------------------------------------------------------------------------------------------------------

    I have edited this script a little bit, its basically a tweaked version from the Default Moving/Jump script posted on Unity-Help Site. I just added the "DEATH" code.
     
  9. Todilo

    Todilo

    Joined:
    Feb 1, 2011
    Posts:
    88
    A very simple boost-functionality(untested)
    Code (csharp):
    1.  
    2. /// This script moves the character controller forward
    3.  
    4. /// and sideways based on the arrow keys.
    5.  
    6. /// It also jumps when pressing space.
    7.  
    8. /// Make sure to attach a character controller to the same game object.
    9.  
    10. /// It is recommended that you make only one call to Move or SimpleMove per frame.
    11.  
    12.  
    13.  
    14. var speed : float = 6.0;
    15.  
    16. var jumpSpeed : float = 8.0;
    17.  
    18. var gravity : float = 20.0;
    19.  
    20. var boostTimer : float = -0.01f;
    21.  
    22. var boostExtra : float = 6.0f;
    23.  
    24. private var dead = false;
    25.  
    26.  
    27.  
    28. function OnControllerColliderHit(hit:ControllerColliderHit)
    29.  
    30. {
    31.  
    32. if(hit.gameObject.tag == "fallout")
    33.  
    34. {
    35.  
    36. dead = true;
    37.  
    38. }
    39.  
    40. }
    41.  
    42.  
    43.  
    44. private var moveDirection : Vector3 = Vector3.zero;
    45.  
    46.  
    47.  
    48. function Update() {
    49.  
    50. var controller : CharacterController = GetComponent(CharacterController);
    51.  
    52. if (controller.isGrounded) {
    53.  
    54. // We are grounded, so recalculate
    55.  
    56. // move direction directly from axes
    57.  
    58. moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
    59.  
    60. Input.GetAxis("Vertical"));
    61.  
    62. moveDirection = transform.TransformDirection(moveDirection);
    63.  
    64. if(boostTime>0.0f)
    65.  
    66. {
    67.  
    68.     // This will give you a static boost. You might want to increase teh boostExtra
    69.  
    70.     // from giving 0 extra in teh beginning but after 1 second give full speedbost, depends on your
    71.  
    72.     //Implementation.
    73.  
    74.     moveDirection *= (speed + boostExtra);
    75.  
    76. }
    77.  
    78. else
    79.  
    80. {
    81.  
    82.     moveDirection *= speed;
    83.  
    84. }
    85.  
    86.  
    87.  
    88. if (Input.GetButton ("Jump")) {
    89.  
    90. moveDirection.y = jumpSpeed;
    91.  
    92. }
    93.  
    94. //Call it whatever you want here
    95.  
    96. if(Input.GetButton("LeftShit")){
    97.  
    98. boostTimer = 3.0f;// The time(in seconds) you want the boost applied for
    99.  
    100. }
    101.  
    102. }
    103.  
    104.  
    105.  
    106. // Apply gravity
    107.  
    108. moveDirection.y -= gravity * Time.deltaTime;
    109.  
    110.  
    111.  
    112. // Move the controller
    113.  
    114. controller.Move(moveDirection * Time.deltaTime);
    115.  
    116. }
    117.  
    118.  
    119.  
    120. function LateUpdate()
    121.  
    122. {
    123.  
    124. if(dead)
    125.  
    126. {
    127.  
    128. transform.position = Vector3(38,4,11);
    129.  
    130. gameObject.Find("Main Camera").transform.position = Vector3(38,4,-11);
    131.  
    132. dead = false;
    133.  
    134. }
    135.  
    136. }
    137.  
    138.  
     
  10. ArminPasalic

    ArminPasalic

    Joined:
    Feb 8, 2011
    Posts:
    58
    It seems to work, but I if I click "Left Shift" as I tweaked code for the button, it seems that the Timer doesnt go lower than 3 Secs. If I click Left Shift I am always boosting... It never stops, and it also says "BoostTimer: -1" wich I added for testing, and it seems to go to "3" and then stay there when I'm boosting.

    It does work with the boost, now we just need fix this timer. :)
     
  11. Todilo

    Todilo

    Joined:
    Feb 1, 2011
    Posts:
    88
    omg I missed a very important line :D, let me fix :D

    Fixed the boost-timer as well as added a cooldown-timer.


    Code (csharp):
    1.  
    2. /// This script moves the character controller forward
    3.  
    4. /// and sideways based on the arrow keys.
    5.  
    6. /// It also jumps when pressing space.
    7.  
    8. /// Make sure to attach a character controller to the same game object.
    9.  
    10. /// It is recommended that you make only one call to Move or SimpleMove per frame.
    11.  
    12.  
    13.  
    14. var speed : float = 6.0;
    15.  
    16. var jumpSpeed : float = 8.0;
    17.  
    18. var gravity : float = 20.0;
    19.  
    20. var boostTimer : float = -0.01f;
    21.  
    22. var boostCooldown : float = -0.01f;
    23.  
    24. var boostExtra : float = 6.0f;
    25.  
    26. private var dead = false;
    27.  
    28.  
    29.  
    30. function OnControllerColliderHit(hit:ControllerColliderHit)
    31.  
    32. {
    33.  
    34. if(hit.gameObject.tag == "fallout")
    35.  
    36. {
    37.  
    38. dead = true;
    39.  
    40. }
    41.  
    42. }
    43.  
    44.  
    45.  
    46. private var moveDirection : Vector3 = Vector3.zero;
    47.  
    48.  
    49.  
    50. function Update() {
    51.  
    52. var controller : CharacterController = GetComponent(CharacterController);
    53.  
    54. if (controller.isGrounded) {
    55.  
    56. // We are grounded, so recalculate
    57.  
    58. // move direction directly from axes
    59.  
    60. moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
    61.  
    62. Input.GetAxis("Vertical"));
    63.  
    64. moveDirection = transform.TransformDirection(moveDirection);
    65.  
    66. if(boostTime>0.0f)
    67.  
    68. {
    69.  
    70.     // This will give you a static boost. You might want to increase teh boostExtra
    71.  
    72.     // from giving 0 extra in teh beginning but after 1 second give full speedbost, depends on your
    73.  
    74.     //Implementation.
    75.  
    76.     moveDirection *= (speed + boostExtra);
    77.  
    78.     boostTimer -= Time.deltaTime;
    79.  
    80. }
    81.  
    82. else
    83.  
    84. {
    85.  
    86.     moveDirection *= speed;
    87.  
    88.     if(boostCooldown>0.0f)
    89.  
    90.         boostCooldown -= Time.deltaTime;
    91.  
    92. }
    93.  
    94.  
    95.  
    96. if (Input.GetButton ("Jump")) {
    97.  
    98. moveDirection.y = jumpSpeed;
    99.  
    100. }
    101.  
    102. //Call it whatever you want here
    103.  
    104. if(Input.GetButton("LeftShit")  (boostCooldown<0.0f)){
    105.  
    106. boostTimer = 3.0f;// The time(in seconds) you want the boost applied for
    107.  
    108. boostCooldown = 5.0f; // Seconds to delay another boost, AFTER the first boost is completed
    109.  
    110. }
    111.  
    112. }
    113.  
    114.  
    115.  
    116. // Apply gravity
    117.  
    118. moveDirection.y -= gravity * Time.deltaTime;
    119.  
    120.  
    121.  
    122. // Move the controller
    123.  
    124. controller.Move(moveDirection * Time.deltaTime);
    125.  
    126. }
    127.  
    128.  
    129.  
    130. function LateUpdate()
    131.  
    132. {
    133.  
    134. if(dead)
    135.  
    136. {
    137.  
    138. transform.position = Vector3(38,4,11);
    139.  
    140. gameObject.Find("Main Camera").transform.position = Vector3(38,4,-11);
    141.  
    142. dead = false;
    143.  
    144. }
    145.  
    146. }
    147.  
    148.  
     
  12. ArminPasalic

    ArminPasalic

    Joined:
    Feb 8, 2011
    Posts:
    58
    Thank you very much! :)

    If you could help with one more thing... I have this "PUSH RIGIDBODY THINGS" so I can move a cube to a destination and then clear a puzzle(for example)

    How can I make the BOOST script to increase the power of the push?

    Here is the Push Rigidbody Things Script:

    --------------------------------------------------------------------------------------------------------------------------------------------------------

    // this script pushes all rigidbodies that the character touches

    var pushPower : float = 2.0;
    function OnControllerColliderHit (hit : ControllerColliderHit) {
    var body : Rigidbody = hit.collider.attachedRigidbody;
    // no rigidbody
    if (body == null || body.isKinematic)
    return;

    // We dont want to push objects below us
    if (hit.moveDirection.y < -0.3)
    return;

    // Calculate push direction from move direction,
    // we only push objects to the sides never up and down
    var pushDir : Vector3 = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);

    // If you know how fast your character is trying to move,
    // then you can also multiply the push velocity by that.

    // Apply the push
    body.velocity = pushDir * pushPower;
    }
     
  13. Todilo

    Todilo

    Joined:
    Feb 1, 2011
    Posts:
    88
    I never use javascript so this is I am afraid beyound me. What you want to do is actually get the script of the object that has pushed you.

    you want get the script and reference a bool inside it that asks if you were boosting. This is only the case if you dont care how fast you were boosting. From what I can see it actually now already works. Because an object being hit gets the hit.moveDirection, and the movedirection is based on both speed+boost.

    The line that does this:
    moveDirection *= (speed + boostExtra);

    affects movedirection so you should be good to go :D.
     
  14. ArminPasalic

    ArminPasalic

    Joined:
    Feb 8, 2011
    Posts:
    58
    Alright then thanks for the Boost Script! It works very well, just need to balance some things out and it's ready to go! :)

    Now the only thing I need now is the Camera Script...
     
  15. Todilo

    Todilo

    Joined:
    Feb 1, 2011
    Posts:
    88
    if I were you I would post another thread about it. Also post some parts of the code and if you can images to display what you want.
     
  16. ArminPasalic

    ArminPasalic

    Joined:
    Feb 8, 2011
    Posts:
    58
  17. ArminPasalic

    ArminPasalic

    Joined:
    Feb 8, 2011
    Posts:
    58
    Todilo, I think I have another problem...

    The script allows to boost and everything and its perfect! But for balancing issues, you can jump with the Boost and then jump randomly to locations without losing the speed, how can I make it so the Player cant jump when he has pressed the required button for the Boost?

    Regards,
    Armin.
     
  18. Todilo

    Todilo

    Joined:
    Feb 1, 2011
    Posts:
    88
    Change:
    if (Input.GetButton ("Jump")) {

    to

    if (Input.GetButton ("Jump") (boostCooldown<0.0f)){

    This will prevent you from jumping when you are boosting, so the boost has to fully regenerate for it to enable jump


    Or if you want to allow just as soon as the boosting no longer i occuring:

    if (Input.GetButton ("Jump") (boostTime<0.0f)){
     
  19. ArminPasalic

    ArminPasalic

    Joined:
    Feb 8, 2011
    Posts:
    58
    Thank you very much! :)
     
  20. ArminPasalic

    ArminPasalic

    Joined:
    Feb 8, 2011
    Posts:
    58
    Todilo, I know I'm being irratating and I'm sorry...

    When the Game starts, the player can't jump untill Sprint is used at LEAST ones. :-(

    I used "if (Input.GetButton ("Jump") (boostTime<0.0f)){"

    Regards.
     
  21. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    set your boostCooldown initially to 0, then when you need to get the boost, set it to whatever value you need it to be.

    about the camera movement script, have you tried attaching it as a child of the ball? it will move and rotate as the ball does + the script will smooth it out (i think this will solve your camera problem, tho i am not sure of it).
     
  22. Todilo

    Todilo

    Joined:
    Feb 1, 2011
    Posts:
    88
    I dont know what causes that because in the top we set boostTime = -0.01f so that is the initial state. This should enable jump to be executed.