Search Unity

How can i merge these 2 scripts to where all functions of both would work no issues.

Discussion in 'Scripting' started by jp8173, Feb 27, 2017.

  1. jp8173

    jp8173

    Joined:
    Feb 24, 2017
    Posts:
    2
    This is the script from the roll a ball game with slight modification.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6. //variables
    7.     public float speed;
    8.     public Text countText;
    9.     public Text winText;
    10.  
    11.     private Rigidbody rb;
    12.     private int count;
    13. //beginning of script/ starting game
    14.     void Start ()
    15.     {
    16.         rb = GetComponent<Rigidbody>();
    17.         count = 0;
    18.         SetCountText ();
    19.         winText.text = "";
    20.     }
    21. //movement
    22.     void FixedUpdate ()
    23.     {
    24.         float moveHorizontal = Input.GetAxis ("Horizontal");
    25.         float moveVertical = Input.GetAxis ("Vertical");
    26.  
    27.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    28.  
    29.         rb.AddForce (movement * speed);
    30.     }
    31. //collecting prisms
    32.     void OnTriggerEnter(Collider other)
    33.     {
    34.         if (other.gameObject.CompareTag ( "Pick Up"))
    35.         {
    36.             other.gameObject.SetActive (false);
    37.             count = count + 1;
    38.             SetCountText ();
    39.         }
    40.     }
    41. //ui control
    42.     void SetCountText ()
    43.     {
    44.         countText.text = "Count: " + count.ToString ();
    45.         if (count >= 12)
    46.         {
    47.             winText.text = "You Win!"; //potentially replaceable by button|add show button script?
    48.         }
    49.     }
    50. }
    This is a jump script only does jumping i believe.

    Code (CSharp):
    1. usingUnityEngine;
    2. usingSystem.Collections;
    3.  
    4. publicclassWasdCont : MonoBehaviour {
    5. //Variables
    6. publicfloatspeed = 6.0F;
    7. publicfloatjumpSpeed = 8.0F;
    8. publicfloatgravity = 20.0F;
    9. privateVector3moveDirection = Vector3.zero;
    10.  
    11. voidUpdate() {
    12. CharacterControllercontroller = GetComponent<CharacterController>();
    13. //isthecontrollerontheground?
    14. if (controller.isGrounded) {
    15. //FeedmoveDirectionwithinput.
    16. moveDirection = newVector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    17. moveDirection = transform.TransformDirection(moveDirection);
    18. //Multiplyitbyspeed.
    19. moveDirection *= speed;
    20. //Jumping
    21. if (Input.GetButton("Jump"))
    22. moveDirection.y = jumpSpeed;
    23.  
    24. }
    25. //Applyinggravitytothecontroller
    26. moveDirection.y -= gravity * Time.deltaTime;
    27. //Makingthecharactermove
    28. controller.Move(moveDirection * Time.deltaTime);
    29. }
    30. }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    The first uses a rigidbody, while the second uses a character controller. You're not going to be able to combine these.

    BTW the second one does lateral movement in addition to jumping.