Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help: Move two player objects simultaneously with Raycast.

Discussion in 'Scripting' started by Harbinger-Czar, Jul 25, 2018.

  1. Harbinger-Czar

    Harbinger-Czar

    Joined:
    Jun 25, 2018
    Posts:
    7
    We have our player controller script setup and working for an individual player object, but when we want to add a second one, we are running into complications. I know we could potentially use a LayerMask to have the RayCast ignore a player object, but when that happens, both objects will try to move into the same space and cause problems. I'm stumped at this point.

    Player Controller Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     public float speed;
    9.     bool isMoving;
    10.     float distance;
    11.     Vector3 endPos;
    12.     public Text parText;
    13.     private int par;
    14.     public static int moves;
    15.     bool upDetect;
    16.     bool downDetect;
    17.     bool rightDetect;
    18.     bool leftDetect;
    19.     Vector3 upLine;
    20.     Vector3 downLine;
    21.     Vector3 leftLine;
    22.     Vector3 rightLine;
    23.     public bool actionCheck;
    24.     Vector3 actionLine;
    25.  
    26.     void Start()
    27.     {
    28.         isMoving = false;
    29.         par = Counter.levelPar;
    30.         endPos = transform.position;
    31.         //setPar();
    32.     }
    33.  
    34.     private void FixedUpdate()
    35.     {
    36.         upLine = new Vector3(transform.position.x, transform.position.y, transform.position.z + 0.1f);
    37.         upDetect = Physics.Linecast(transform.position, upLine);
    38.         Debug.DrawLine(transform.position, upLine);
    39.  
    40.         downLine = new Vector3(transform.position.x, transform.position.y, transform.position.z - 0.1f);
    41.         downDetect = Physics.Linecast(transform.position, downLine);
    42.         Debug.DrawLine(transform.position, downLine);
    43.  
    44.         leftLine = new Vector3(transform.position.x - 0.1f, transform.position.y, transform.position.z);
    45.         leftDetect = Physics.Linecast(transform.position, leftLine);
    46.         Debug.DrawLine(transform.position, leftLine);
    47.  
    48.         rightLine = new Vector3(transform.position.x + 0.1f, transform.position.y, transform.position.z);
    49.         rightDetect = Physics.Linecast(transform.position, rightLine);
    50.         Debug.DrawLine(transform.position, rightLine);
    51.  
    52.        // actionLine = new Vector3(transform.position.x, transform.position.y - 1, transform.position.z);
    53.        // actionCheck = Physics.Linecast(transform.position, actionLine);
    54.        // Debug.DrawLine(transform.position, actionLine);
    55.  
    56.         if (Input.GetKey("left") && isMoving == false && leftDetect == false)
    57.         {
    58.             isMoving = true;
    59.             RaycastHit hit;
    60.             Ray leftRay = new Ray(transform.position, Vector3.left);
    61.             if (Physics.Raycast(leftRay, out hit))
    62.             {
    63.                 if (hit.collider != null)
    64.                 {
    65.                     endPos = new Vector3(hit.collider.transform.position.x + 1, endPos.y, endPos.z);
    66.                 }
    67.             }
    68.             //countMove();
    69.         }
    70.  
    71.         if (Input.GetKey("right") && isMoving == false && rightDetect == false)
    72.         {
    73.             isMoving = true;
    74.             RaycastHit hit;
    75.             Ray rightRay = new Ray(transform.position, Vector3.right);
    76.             if (Physics.Raycast(rightRay, out hit))
    77.             {
    78.                 if (hit.collider != null)
    79.                 {
    80.                     endPos = new Vector3(hit.collider.transform.position.x - 1, endPos.y, endPos.z);
    81.                 }
    82.             }
    83.             //countMove();
    84.         }
    85.  
    86.         if (Input.GetKey("up") && isMoving == false && upDetect == false)
    87.         {
    88.             isMoving = true;
    89.             RaycastHit hit;
    90.             Ray upRay = new Ray(transform.position, Vector3.forward);
    91.             if (Physics.Raycast(upRay, out hit))
    92.             {
    93.                 if (hit.collider != null)
    94.                 {
    95.                     endPos = new Vector3(endPos.x, endPos.y, hit.collider.transform.position.z - 1);
    96.                 }
    97.             }
    98.             //countMove();
    99.         }
    100.  
    101.         if (Input.GetKey("down") && isMoving == false && downDetect == false)
    102.         {
    103.             isMoving = true;
    104.             RaycastHit hit;
    105.             Ray downRay = new Ray(transform.position, -Vector3.forward);
    106.             if (Physics.Raycast(downRay, out hit))
    107.             {
    108.                 if (hit.collider != null)
    109.                 {
    110.                     endPos = new Vector3(endPos.x, endPos.y, hit.collider.transform.position.z + 1);
    111.                 }
    112.             }
    113.             //countMove();
    114.         }
    115.  
    116.  
    117.  
    118.         distance = Vector3.Distance(transform.position, endPos);
    119.         //Debug.Log(distance);
    120.         //Debug.Log(rightDetect);
    121.         //Debug.Log(leftDetect);
    122.         //Debug.Log(upDetect);
    123.         //Debug.Log(downDetect);
    124.  
    125.         if (distance < 0.01)
    126.         {
    127.             distance = 0;
    128.         }
    129.  
    130.         if (distance > 0)
    131.         {
    132.             transform.position = Vector3.Lerp(
    133.                 transform.position, endPos,
    134.                 Time.deltaTime * speed / distance);
    135.             transform.rotation = Quaternion.identity;
    136.         }
    137.         else
    138.         {
    139.             isMoving = false;
    140.             endPos = transform.position;
    141.         }
    142.  
    143.         //setPar();
    144.  
    145.     }
    146.  
    147.     /*void countMove()
    148.     {
    149.         moves++;
    150.  
    151.         if (par > 0)
    152.         {
    153.             par--;
    154.         }
    155.     }
    156.  
    157.     void setPar()
    158.     {
    159.         parText.text = "Par: " + par.ToString();
    160.     }*/
    161.  
    162. }
     
  2. Harbinger-Czar

    Harbinger-Czar

    Joined:
    Jun 25, 2018
    Posts:
    7
    Figured it out. I had to use the Raycast hit to figure out what object it had hit. If the object had a "Player" tag, then it would go to that object's PlayerController script and get the value for it's end pos. Then I just had to adjust the endPos variable to compensate for the extra space. Here's my code.

    PlayerController:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8. public float speed;
    9. bool isMoving;
    10. public float distance;
    11. public Vector3 endPos;
    12. public Text parText;
    13. private int par;
    14. public static int moves;
    15. bool upDetect;
    16. bool downDetect;
    17. bool rightDetect;
    18. bool leftDetect;
    19. Vector3 upLine;
    20. Vector3 downLine;
    21. Vector3 leftLine;
    22. Vector3 rightLine;
    23. Vector3 actionLine;
    24. Rigidbody rb;
    25.  
    26. void Start()
    27. {
    28.  
    29. isMoving = false;
    30. par = Counter.levelPar;
    31. endPos = transform.position;
    32. rb = GetComponent<Rigidbody>();
    33.  
    34. //setPar();
    35. }
    36.  
    37. private void FixedUpdate()
    38. {
    39. upLine = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1);
    40. upDetect = Physics.Linecast(transform.position, upLine);
    41. Debug.DrawLine(transform.position, upLine);
    42.  
    43. downLine = new Vector3(transform.position.x, transform.position.y, transform.position.z - 1);
    44. downDetect = Physics.Linecast(transform.position, downLine);
    45. Debug.DrawLine(transform.position, downLine);
    46.  
    47. leftLine = new Vector3(transform.position.x - 1, transform.position.y, transform.position.z);
    48. leftDetect = Physics.Linecast(transform.position, leftLine);
    49. Debug.DrawLine(transform.position, leftLine);
    50.  
    51. rightLine = new Vector3(transform.position.x + 1, transform.position.y, transform.position.z);
    52. rightDetect = Physics.Linecast(transform.position, rightLine);
    53. Debug.DrawLine(transform.position, rightLine);
    54.  
    55. // actionLine = new Vector3(transform.position.x, transform.position.y - 1, transform.position.z);
    56. // actionCheck = Physics.Linecast(transform.position, actionLine);
    57. // Debug.DrawLine(transform.position, actionLine);
    58.  
    59. if (Input.GetKey("left") && isMoving == false && leftDetect == false)
    60. {
    61. isMoving = true;
    62. RaycastHit hit;
    63. Ray leftRay = new Ray(transform.position, Vector3.left);
    64. if (Physics.Raycast(leftRay, out hit))
    65. {
    66. if (hit.collider != null && hit.collider.tag != "Player")
    67. {
    68. endPos = new Vector3(hit.collider.transform.position.x + 1, endPos.y, endPos.z);
    69. }
    70.  
    71. if (hit.collider.tag == "Player")
    72. {
    73. GameObject oPlayer = hit.collider.gameObject;
    74. endPos = GetOtherEndPos(oPlayer);
    75. endPos = new Vector3(endPos.x + 1, endPos.y, endPos.z);
    76. }
    77. }
    78. //countMove();
    79. }
    80.  
    81. if (Input.GetKey("right") && isMoving == false && rightDetect == false)
    82. {
    83. isMoving = true;
    84. RaycastHit hit;
    85. Ray rightRay = new Ray(transform.position, Vector3.right);
    86. if (Physics.Raycast(rightRay, out hit))
    87. {
    88. if (hit.collider != null && hit.collider.tag != "Player")
    89. {
    90. endPos = new Vector3(hit.collider.transform.position.x - 1, endPos.y, endPos.z);
    91. }
    92.  
    93. if (hit.collider.tag == "Player")
    94. {
    95. GameObject oPlayer = hit.collider.gameObject;
    96. endPos = GetOtherEndPos(oPlayer);
    97. endPos = new Vector3(endPos.x - 1, endPos.y, endPos.z);
    98. }
    99. }
    100. //countMove();
    101. }
    102.  
    103. if (Input.GetKey("up") && isMoving == false && upDetect == false)
    104. {
    105. isMoving = true;
    106. RaycastHit hit;
    107. Ray upRay = new Ray(transform.position, Vector3.forward);
    108. if (Physics.Raycast(upRay, out hit))
    109. {
    110. if (hit.collider != null && hit.collider.tag != "Player")
    111. {
    112. endPos = new Vector3(endPos.x, endPos.y, hit.collider.transform.position.z - 1);
    113. }
    114.  
    115. if (hit.collider.tag == "Player")
    116. {
    117. GameObject oPlayer = hit.collider.gameObject;
    118. endPos = GetOtherEndPos(oPlayer);
    119. endPos = new Vector3(endPos.x , endPos.y, endPos.z - 1);
    120. }
    121. }
    122. //countMove();
    123. }
    124.  
    125. if (Input.GetKey("down") && isMoving == false && downDetect == false)
    126. {
    127. isMoving = true;
    128. RaycastHit hit;
    129. Ray downRay = new Ray(transform.position, -Vector3.forward);
    130. if (Physics.Raycast(downRay, out hit))
    131. {
    132. if (hit.collider != null && hit.collider.tag != "Player")
    133. {
    134. endPos = new Vector3(endPos.x, endPos.y, hit.collider.transform.position.z + 1);
    135. }
    136.  
    137. if (hit.collider.tag == "Player")
    138. {
    139. GameObject oPlayer = hit.collider.gameObject;
    140. endPos = GetOtherEndPos(oPlayer);
    141. endPos = new Vector3(endPos.x, endPos.y, endPos.z + 1);
    142. }
    143. }
    144. //countMove();
    145. }
    146.  
    147. distance = Vector3.Distance(transform.position, endPos);
    148. //Debug.Log(distance);
    149. //Debug.Log("Name: " + this.name + " distance = " + distance + " vel = " + rb.velocity.magnitude + " isMoving: " + isMoving);
    150. //Debug.Log(rightDetect);
    151. //Debug.Log(leftDetect);
    152. //Debug.Log(upDetect);
    153. //Debug.Log(downDetect);
    154. //Debug.Log(isMoving);
    155. //Debug.Log("Velocity = " + rb.velocity.magnitude);
    156.  
    157. if (distance < 0.1)
    158. {
    159. distance = 0;
    160. }
    161.  
    162. if (distance > 0)
    163. {
    164. transform.position = Vector3.Lerp(
    165. transform.position, endPos,
    166. Time.deltaTime * speed / distance);
    167. transform.rotation = Quaternion.identity;
    168. }
    169.  
    170. else
    171. {
    172. isMoving = false;
    173. endPos = transform.position;
    174. }
    175.  
    176. //setPar();
    177.  
    178. }
    179.  
    180. Vector3 GetOtherEndPos(GameObject oPlayer)
    181. {
    182. PlayerController script = oPlayer.GetComponent<PlayerController>();
    183. return script.endPos;
    184. }
    185.  
    186. /*void countMove()
    187. {
    188. moves++;
    189.  
    190. if (par > 0)
    191. {
    192. par--;
    193. }
    194. }
    195.  
    196. void setPar()
    197. {
    198. parText.text = "Par: " + par.ToString();
    199. }*/
    200.  
    201. }