Search Unity

Brackey's RPG Movement Help

Discussion in 'Community Learning & Teaching' started by Xayias, Mar 23, 2019.

  1. Xayias

    Xayias

    Joined:
    Mar 22, 2019
    Posts:
    7
    Hello all, I am fairly new to Unity and I am following along with the RPG series that Brackeys made with great ease. The only thing that has not been so nice is that when I click for my character to go to a point on the ground, he will move there but instead of just standing at that point, he will keep moving side to side at the point.



    Hopefully you are able to look at what I am describing. Anyway I have gone back to look at all my scripts. I am basically all the way through the third video and next up is Items. I want to flesh out things before adding more so I figured I would post here and see if anyone might have some helpful insight, thanks much!


    PlayerMotor Script I Have

    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.AI;
    7.  
    8. [RequireComponent(typeof(NavMeshAgent))]
    9. public class PlayerMotor : MonoBehaviour
    10. {
    11. Transformtarget; // Target to follow
    12. NavMeshAgentagent; // Reference to our agent
    13. // Start is called before the first frame update
    14. voidStart()
    15. {
    16. agent=GetComponent<NavMeshAgent>();
    17. }
    18.  
    19. voidUpdate()
    20. {
    21. if (target!=null)
    22. {
    23. agent.SetDestination(target.position);
    24. FaceTarget();
    25. }
    26. }
    27.  
    28. publicvoidMoveToPoint (Vector3point)
    29. {
    30. agent.SetDestination(point);
    31. }
    32.  
    33. publicvoidFollowTarget(InteractablenewTarget)
    34. {
    35. agent.stoppingDistance=newTarget.radius* .8f;
    36. agent.updateRotation=false;
    37. target=newTarget.interactionTransform;
    38. }
    39.  
    40. publicvoidStopFollowingTarget ()
    41. {
    42. agent.stoppingDistance=0f;
    43. agent.updateRotation=true;
    44. target=null;
    45. }
    46.  
    47. voidFaceTarget ()
    48. {
    49. Vector3direction= (target.position-transform.position).normalized;
    50. QuaternionlookRotation=Quaternion.LookRotation(newVector3(direction.x, 0f, direction.z));
    51. transform.rotation=Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime*5f);
    52. }
    53. }
    54.  
    55.  
    56.  

    PlayerController


    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. [RequireComponent(typeof(PlayerMotor))]
    8. public class PlayerController : MonoBehaviour
    9. {
    10. publicInteractablefocus;
    11. publicLayerMaskmovementMask;
    12. Cameracam;
    13. PlayerMotormotor;
    14.  
    15. // Start is called before the first frame update
    16. voidStart()
    17. {
    18. cam=Camera.main;
    19. motor=GetComponent<PlayerMotor>();
    20. }
    21.  
    22. // Update is called once per frame
    23. voidUpdate()
    24. {
    25. if (Input.GetMouseButtonDown(0))
    26. {
    27. Rayray=cam.ScreenPointToRay(Input.mousePosition);
    28. RaycastHithit;
    29.  
    30. if(Physics.Raycast(ray, outhit, 100, movementMask))
    31. {
    32. // Move our player to what we hit
    33. motor.MoveToPoint(hit.point);
    34. // Stop focusing any objects
    35. RemoveFocus();
    36. }
    37. }
    38.  
    39. if (Input.GetMouseButtonDown(1))
    40. {
    41. Rayray=cam.ScreenPointToRay(Input.mousePosition);
    42. RaycastHithit;
    43.  
    44. if(Physics.Raycast(ray, outhit, 100))
    45. {
    46. // Check if we hit an interactable
    47. Interactableinteractable=hit.collider.GetComponent<Interactable>();
    48. // If we did set it as our focus
    49. if (interactable!=null)
    50. {
    51. SetFocus(interactable);
    52. }
    53. }
    54. }
    55. }
    56.  
    57. voidSetFocus (InteractablenewFocus)
    58. {
    59. if(newFocus!=focus)
    60. {
    61. if (focus!=null)
    62. focus.OnDefocused();
    63.  
    64. focus=newFocus;
    65. motor.FollowTarget(newFocus);
    66. }
    67.  
    68. newFocus.OnFocused(transform);
    69. }
    70.  
    71. voidRemoveFocus ()
    72. {
    73. if (focus!=null)
    74. focus.OnDefocused();
    75.  
    76. focus=null;
    77. motor.StopFollowingTarget();
    78. }
    79. }
    80.  
    81.  
     
  2. Xayias

    Xayias

    Joined:
    Mar 22, 2019
    Posts:
    7
    I found the issue. For some reason the Auto Braking for the NavMeshAgent wasn't turned on when I created the compnent, is that supposed to be off by default?