Search Unity

Runtime Object Manipulation

Discussion in 'Works In Progress - Archive' started by Aidenjl, Nov 23, 2014.

?

Would you buy this if it was on the Asset Store?

  1. Yes

    100.0%
  2. No

    0 vote(s)
    0.0%
  1. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    Hello!

    So the reason I am uploading this is due to a recent post I saw about someone wanting to build a jenga game in Unity, As I allready had a project I had been working on to replicate the halo forge system I decided that I would adapt this to work on anything that required moving objects in-game.

    As I started work on this a long time ago the scripts are pretty messy, I will try my best to polish this up as much as possible in the future.

    Here's a screenshot of the system:
    upload_2014-11-23_18-20-25.png

    It works on both Rigidbodies and static objects. You can do things such as throwing, placing and many more features you probably won't use.

    Just out of interest I would like to create a poll to see if people would buy something like this if I put it on the asset store, because y'know I don't want to work on something and it turns out being a waste of time.

    Anyway, for the scripts:

    Manager:

    Code (JavaScript):
    1. #pragma strict
    2. //GameObjects//
    3. var PlaceableGo : GameObject; // The Transform that handles the rotation and position of the selected object
    4. var PlayerGo : GameObject; // The Player
    5. //ScrollWheel//
    6. var ScrollWheel : boolean; //Whether you want to be able to move the object with the scroll wheel
    7. var ScrollSpeed : float = 5; // The speed at which the object moves with the scrollwheel
    8. var StopPoint : boolean; // Do we want the object to stop moving towards us at a certain point?
    9. var MinimumDistance : float = 1; //The distance at which the object stops coming towards us
    10. //Positioning//
    11. var FixedPosition : boolean; //Do you want the objects to allways go to the same position?
    12. //Pick Distance//
    13. var PickDistance : boolean; // Do you want there to be a maximum distance that you can pick the object up from?
    14. var MaximumDistance : float = 10; //Maximum pick distance
    15. //DropDistance//
    16. var DropDistance : boolean; // Do you want the player to drop the item when it is a certain distance away from them?
    17. var DropLength : float; //The distance that you drop the object at
    18. var Distance : float;
    19. //Rotation//
    20. var FixedRotation : boolean; //Do you want the objects to allways go to the same rotation?
    21. var RelativeRotate : boolean;// Do you want objects to rotate based on the camera's rotation?
    22. //Throw//
    23. var Throw : boolean = true; // Will we throw the Rigidbody when we let go?
    24. var ThrowForce : float = 1000; // The force that the Rigidbody will be thrown at
    25. //Materials//
    26. var ChangeMaterial : boolean; // Will we change the item's shader once we have picked it up?
    27. var ItemMaterial : Material; //The shader the Item changes to once it is picked up
    28. private var MaterialCache : Material; // Where we store the original shader
    29. private var TextureCache : Texture; // Where we store the original texture
    30. //Smooth Follow//
    31. var ObjectSmoothFollow : boolean;//Whether you want the non Rigidbody to follow the camera or snap to it
    32. var RigidBodySmoothFollow : boolean;//Whether you want the Rigidbody to follow the camera or snap to it
    33. var SmoothTime : float; //The speed at which it follows
    34. //Important Shizzle//
    35. private var ReturnPoint : Vector3; // The Point that the PlaceableGo returns to
    36. private var ReturnRotate : Quaternion; // The Rotation that the PlaceableGo returns to
    37. private var SelectedObject : GameObject;
    38. var IsLatched : boolean = false; // If you have picked up an object
    39. private var PlaceScript : Placeable; // A script referance to the one on the object that you can pick up
    40. private var RigidbodyGo : Rigidbody;
    41. private var SpawnlistOpen : boolean = true; // Unused at the moment
    42. private var SelectedObjectCache : GameObject;
    43. //ErrorHandling//
    44. private var ErrorOccurred : boolean = false;
    45.  
    46.  
    47.  
    48. function Start () {
    49.  
    50. ReturnPoint = PlaceableGo.transform.localPosition; // Set the return postion to the start position
    51. ReturnRotate = PlaceableGo.transform.rotation; // Set the return rotation to the start rotation
    52.  
    53. Screen.showCursor = false; // Hide the cursor
    54. }
    55.  
    56. function Update () {
    57.  
    58. Distance = PlaceableGo.transform.localPosition.z - transform.localPosition.z; // Find the distance between the object and the player
    59.  
    60. if (ScrollWheel == true){
    61. if (Input.GetAxis("Mouse ScrollWheel")){
    62. PlaceableGo.transform.localPosition.z+= Input.GetAxis("Mouse ScrollWheel") * ScrollSpeed; // Move the selected object forward relative to the camera
    63. if (StopPoint == true){
    64. if (Distance < MinimumDistance){ //Check if the object has gone past the minimum distance
    65. PlaceableGo.transform.localPosition.z = transform.localPosition.z + MinimumDistance; //Make sure that the object can not go past the minimum distance
    66. }
    67. }
    68. }
    69. }
    70.  
    71. if (DropDistance == true){
    72. if (Distance > DropLength){ //Checks if the distance of the object is larger than the drop distance
    73. UnLatch(); // If the distance id larger than the drop distance it calls the UnLatch function and drops the object
    74. }
    75. }
    76.  
    77. if (Input.GetButtonDown("Fire1")){
    78. if (IsLatched == true){
    79. UnLatch(); // Drop the object
    80. } else if (IsLatched == false){
    81. var ray : Ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0));
    82. var hit : RaycastHit;
    83.  
    84. if (PickDistance == true){
    85. if (Physics.Raycast(ray, hit, MaximumDistance)){
    86. SelectedObject = hit.transform.gameObject;
    87. Latch(); // Pick up the object
    88. return;
    89. }
    90. } else {
    91. if (Physics.Raycast(ray, hit)){
    92. SelectedObject = hit.transform.gameObject;
    93. Latch(); // Pick up the object
    94. return;
    95. }
    96. }
    97. }
    98. }
    99.  
    100. //Rotation//
    101.  
    102. if (IsLatched == true && Input.GetKeyDown("left") && Input.GetKey("left shift")){
    103. PlaceableGo.transform.Rotate(0,5,0);
    104. }else if (IsLatched == true && Input.GetKeyDown("left")){
    105. PlaceableGo.transform.Rotate(0,90,0);
    106. }
    107.  
    108. if (IsLatched == true && Input.GetKeyDown("right") && Input.GetKey("left shift")){
    109. PlaceableGo.transform.Rotate(0,-5,0);
    110. }else if (IsLatched == true && Input.GetKeyDown("right")){
    111. PlaceableGo.transform.Rotate(0,-90,0);
    112. }
    113.  
    114. if (IsLatched == true && Input.GetKeyDown("up") && Input.GetKey("left shift")){
    115. //SelectedObject.transform.Rotate(5,0,0);
    116. SelectedObject.transform.eulerAngles.x += 5;
    117. }else if (IsLatched == true && Input.GetKeyDown("up")){
    118. SelectedObject.transform.Rotate(90,0,0);
    119. }
    120.  
    121. if (IsLatched == true && Input.GetKeyDown("down") && Input.GetKey("left shift")){
    122. SelectedObject.transform.Rotate(-5,0,0);
    123. }else if (IsLatched == true && Input.GetKeyDown("down")){
    124. SelectedObject.transform.Rotate(-90,0,0);
    125. }
    126. }
    127.  
    128. function Latch() {
    129.     if (SelectedObject.GetComponent("Placeable")){ //Checks if the selected object has the "Placeable Script"
    130.     SelectedObjectCache = SelectedObject; //Stores the object incase it encounters any errors
    131.     if (ChangeMaterial == true){
    132.     MaterialCache = SelectedObject.renderer.material; // stores the original shader so we can change it back later
    133.     TextureCache = SelectedObject.renderer.material.mainTexture; // Stores the orignal Texture
    134.     SelectedObject.renderer.material = ItemMaterial; // Changes the Object's shader to the specified one
    135.     SelectedObject.renderer.material.mainTexture = TextureCache; // Changes the texture of the new material to the original texture of the object
    136.     }
    137.     if (SelectedObject.rigidbody){ //Checks if the selected object is a rigidbody
    138.     SelectedObject.rigidbody.isKinematic = true; //Stops the selected object from acting like a rigidbody whilst it is selected
    139.     //SelectedObject.rigidbody.freezeRotation = true;
    140.    // SelectedObject.rigidbody.useGravity = false;
    141.     }
    142.     PlaceScript = SelectedObject.GetComponent("Placeable"); //Attatches itself to the selected object's "Placeable Script"
    143.     if (FixedPosition == false){
    144.     PlaceableGo.transform.position = SelectedObject.transform.position; //Moves the PlaceableGo to the position of the selected object
    145.     }
    146.     if (FixedRotation == false){
    147.     PlaceableGo.transform.rotation.eulerAngles = SelectedObject.transform.rotation.eulerAngles; //Changes the rotation of PlaceableGo to match the rotation of the selected object
    148.     }
    149.     PlaceScript.startChildRotationQ = SelectedObject.transform.rotation;
    150.     if (RigidBodySmoothFollow == true){
    151.     PlaceScript.RigidBodySmoothFollow = true;
    152.     PlaceScript.smoothTime = SmoothTime;
    153.     } else {
    154.     PlaceScript.RigidBodySmoothFollow = false;
    155.     }
    156.     if (ObjectSmoothFollow == true){
    157.     PlaceScript.ObjectSmoothFollow = true;
    158.     PlaceScript.smoothTime = SmoothTime;
    159.     } else {
    160.     PlaceScript.ObjectSmoothFollow = false;
    161.     }
    162.     if (RelativeRotate == true){
    163.     PlaceScript.RelativeRotate = true;
    164.     }else{
    165.     PlaceScript.RelativeRotate = false;
    166.     }
    167.     PlaceScript.IsLatched = true; //Tells the object that it has been picked up
    168.     IsLatched = true;
    169. }
    170. }
    171.  
    172. function UnLatch() {
    173.     //ErrorHandling//
    174.     if (SelectedObject == null){
    175.     SelectedObject = SelectedObjectCache;
    176.     ErrorOccurred = true;
    177.     }
    178.     //ErrorHandling//
    179.     if (PlaceScript == null){
    180.     PlaceScript = SelectedObject.GetComponent("Placeable");
    181.     }
    182.  
    183.     if (ChangeMaterial == true){
    184.     //ErrorHandling//
    185.     if (MaterialCache == null){
    186.     MaterialCache = PlaceScript.StartMat;
    187.     }
    188.     //ErrorHandling//
    189.     if (TextureCache == null){
    190.     TextureCache = PlaceScript.StartTex;
    191.     }
    192.     SelectedObject.renderer.material = MaterialCache; // Resets the Object's shader to it's original shader
    193.     SelectedObject.renderer.material.mainTexture = TextureCache; // Changes the texture of the new material to the original texture of the object
    194.     MaterialCache = null; // Resets the shader Cache
    195.     TextureCache = null; // Resets the texture Cache
    196.     }
    197.     PlaceScript.IsLatched = false;
    198.     if (SelectedObject.rigidbody){
    199.     SelectedObject.rigidbody.isKinematic = false;
    200.     //SelectedObject.rigidbody.freezeRotation = false;
    201.     //SelectedObject.rigidbody.useGravity = true;
    202.     if (Throw == true){
    203.     if (ErrorOccurred == false){ // If an error has occurred this stops it from repeating this script
    204.     SelectedObject.rigidbody.AddForceAtPosition(transform.forward * ThrowForce, PlaceableGo.transform.position);
    205.     }
    206.     }
    207.     }
    208.     IsLatched = false;
    209.     PlaceScript = null;
    210.     SelectedObject = null;
    211.     PlaceableGo.transform.localPosition = ReturnPoint;
    212.     PlaceableGo.transform.rotation = ReturnRotate;
    213.     ErrorOccurred = false; // This is called now that the error has been dealt with
    214.     return;
    215. }
    216.  
    217.  
    218.  
    219.  
    Placeable:

    Code (JavaScript):
    1. #pragma strict
    2. public var PlaceableGo : GameObject;
    3. public var PlayerGo : GameObject;
    4. var RelativeRotate : boolean;
    5. var ObjectSmoothFollow : boolean;//Whether you want the non Rigidbody to follow the camera or snap to it
    6. var RigidBodySmoothFollow : boolean;//Whether you want the Rigidbody to follow the camera or snap to it
    7. var smoothTime = 20; //Rigidbody movement speed
    8. //ErrorHandling//
    9. var StartMat : Material; //The starting material is saved in case of any errors
    10. var StartTex : Texture; //The starting Texture is saved in case of any errors
    11.  
    12. var IsLatched : boolean = false;//Has it been picked up?
    13.  
    14. public var startChildRotationQ: Quaternion; //Start rotation of the object
    15. private var ChildRotation : Quaternion; //Current rotation of the object
    16.  
    17. function Start () {
    18. //ErrorHandling//
    19. StartMat = renderer.material; // stores the original Material
    20. StartTex = renderer.material.mainTexture; // Stores the orignal Texture
    21. }
    22.  
    23. function Update () {
    24.  
    25. if (IsLatched == true){
    26. if (gameObject.rigidbody){
    27. if (RigidBodySmoothFollow == true){
    28. transform.position.x = Mathf.Lerp( transform.position.x, PlaceableGo.transform.position.x, Time.deltaTime * smoothTime);//Smoothfollow along the x Axis
    29. transform.position.y = Mathf.Lerp( transform.position.y, PlaceableGo.transform.position.y, Time.deltaTime * smoothTime);//Smoothfollow along the y Axis
    30. transform.position.z = Mathf.Lerp( transform.position.z, PlaceableGo.transform.position.z, Time.deltaTime * smoothTime);//Smoothfollow along the z Axis
    31. } else {
    32. transform.rigidbody.MovePosition(PlaceableGo.transform.position);
    33. //transform.position = PlaceableGo.transform.position;//Constantly matches the postion of PlaceableGo instead of a smoothfollow
    34. }
    35. if (RelativeRotate == true){
    36. transform.rotation.eulerAngles.y = PlaceableGo.transform.rotation.eulerAngles.y;//Matches the rotation of the PlaceableGo to keep the rotation relative to the camera
    37. }
    38. }else{
    39. if (RelativeRotate == true){
    40. transform.rotation.eulerAngles.y = PlaceableGo.transform.rotation.eulerAngles.y;//Matches the rotation of the PlaceableGo to keep the rotation relative to the camera
    41. }
    42. if (ObjectSmoothFollow == true){
    43. transform.position.x = Mathf.Lerp( transform.position.x, PlaceableGo.transform.position.x, Time.deltaTime * smoothTime);//Smoothfollow along the x Axis
    44. transform.position.y = Mathf.Lerp( transform.position.y, PlaceableGo.transform.position.y, Time.deltaTime * smoothTime);//Smoothfollow along the y Axis
    45. transform.position.z = Mathf.Lerp( transform.position.z, PlaceableGo.transform.position.z, Time.deltaTime * smoothTime);//Smoothfollow along the z Axis
    46. } else {
    47. transform.position = PlaceableGo.transform.position;//Constantly matches the postion of PlaceableGo instead of a smoothfollow
    48. }
    49. }
    50. }
    51. }
    52.  
    53. function Destroy (){
    54.  
    55. }
    56.  
    57. //function OnCollisionEnter(collision : Collision) {
    58. //if (IsLatched == true){
    59. //var contact : ContactPoint = collision.contacts[0];
    60. //var pos : Vector3 = contact.point;
    61. //PlaceableGo.transform.position.z = pos.z - 1;
    62. //}
    63. //}
    64.  
    65.  
    HUD:

    Code (JavaScript):
    1. #pragma strict
    2. private var SelectedObject : GameObject;
    3. var Reticule : Texture;
    4. var SelectReticule : Texture;
    5. var Hud : Texture;
    6. private var ManagerScript : Manager;
    7. var ManagerGo : GameObject;
    8.  
    9. function Start () {
    10. Hud = Reticule;
    11. ManagerScript = ManagerGo.GetComponent("Manager");
    12. }
    13.  
    14. function Update () {
    15. var ray : Ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0));
    16. var hit : RaycastHit;
    17. if (Physics.Raycast(ray, hit)){
    18. SelectedObject = hit.transform.gameObject;
    19. if (SelectedObject.GetComponent("Placeable") || ManagerScript.IsLatched == true){
    20. Hud = SelectReticule;
    21. }else{
    22. Hud = Reticule;
    23. }
    24. } else {
    25. if (ManagerScript.IsLatched == false){
    26. Hud = Reticule;
    27. }
    28. }
    29. }
    30.  
    31. function OnGUI(){
    32. GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height), Hud);
    33. }
    34.  
    35.  
    (Make sure you used the righ code names, it's vital)

    Setting up:
    1. Setup a First Person Controller
    2. Attatch the manager script to the camera on the First Person Controller
    3. Create an empty game object as a child of the camera and place it roughly infront of the camera
    4. Go back to the Manager script on the camera and set the PlaceableGo as the child object of the camera
    5. Set the PlayerGo as the First Person Controller
    6. upload_2014-11-23_18-27-34.png
    7. Place the HUD script on the camera (It's pretty self explanitory to setup, if you do have any troubles don't hesitate to tell me)
    8. To make a moveable object just drag the Placeable script onto it(Remember if you want it to have physics to put a Rigidbody on it)
    9. upload_2014-11-23_18-30-19.png
    That's it, if you have any problems or suggestions feel free to comment :)
     
    Last edited: Nov 23, 2014
  2. LukeDodds

    LukeDodds

    Joined:
    Oct 24, 2014
    Posts:
    46
    hey there having two errors in the manager script
    error one: Type expected
    Code (csharp):
    1.  
    2. var PlaceableGo : GameObject;
    3.  
    and the second error: Unexpected symbol :
    Code (csharp):
    1.  
    2. var ray : Ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0));
    3. var hit : RaycastHit;
    4.  
    5.  
     
  3. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    This may seem stupid, but you have put this into JS right?
     
  4. LukeDodds

    LukeDodds

    Joined:
    Oct 24, 2014
    Posts:
    46
    ahhh no i was hoping to use C#
     
  5. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    I can convert it if you would like?
     
  6. LukeDodds

    LukeDodds

    Joined:
    Oct 24, 2014
    Posts:
    46
    I would really appreciate it if you wouldnt mind, its the language im learning at the moment so im worried if this is in Java it will confuse me between the two and its syntax
     
  7. LukeDodds

    LukeDodds

    Joined:
    Oct 24, 2014
    Posts:
    46
    Hey any update on this?