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

Non Fluid Character Movement

Discussion in 'Scripting' started by Paykoman, Jan 18, 2015.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys..

    I'm having some issues in what referes with my character movement.. The question is, im using 1st navemesh to move the character if im in image location and try to go point A everythings goes well if i dont try back before it reachs the A point. If i click to go point A and before it takes it i click to point B instead of just turn and move the player describes that kinda of curve and before it curve it runs a bit forward first and then it goes fine at B point. Wt i would like to know is if this is a scripting problem or since im using a squeleton asset could be the animation that come with problem but i really dont thing thats an animation problem. If someone can help i will appreciate..

    I will post the script and the image.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(NavMeshAgent))]
    4.  
    5. public class Move : MonoBehaviour
    6. {
    7.     public AnimationClip idle;
    8.     public AnimationClip run;
    9.  
    10.     public enum MouseButtonType {Left, Right, Middle};
    11.     public MouseButtonType mouseButton = MouseButtonType.Left;
    12.  
    13.     public Transform myTransform;
    14.     private NavMeshAgent navMeshAgent;
    15.     private Animation anim;
    16.  
    17.     void Awake()
    18.     {
    19.         myTransform = transform;
    20.         anim = GetComponent<Animation> ();
    21.         navMeshAgent = GetComponent<NavMeshAgent> ();
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update ()
    26.     {
    27.         if (navMeshAgent.remainingDistance < 0.5f)
    28.         {
    29.             if (idle != null && anim != null) anim.CrossFade (idle.name);      
    30.         } else
    31.         {
    32.             if(run != null && anim != null) anim.CrossFade (run.name);  
    33.         }
    34.  
    35.         //Move player if left mouse button is clicked
    36.         if (Input.GetMouseButtonDown ((int)mouseButton) && GUIUtility.hotControl == 0)
    37.         {
    38.             Plane playerPlane = new Plane (Vector3.up, myTransform.position);
    39.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    40.             float hitdist = 0.0f;
    41.  
    42.             if (playerPlane.Raycast (ray, out hitdist))
    43.             {
    44.                 navMeshAgent.SetDestination (ray.GetPoint (hitdist));
    45.             }
    46.         }
    47.  
    48.         //Move player if the mouse button is held down
    49.         else if (Input.GetMouseButton ((int)mouseButton) && GUIUtility.hotControl == 0)
    50.         {
    51.             Plane playerPlane = new Plane (Vector3.up, myTransform.position);  
    52.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    53.             float hitdist = 0.0f;
    54.  
    55.             if (playerPlane.Raycast (ray, out hitdist))
    56.             {
    57.                 navMeshAgent.SetDestination (ray.GetPoint (hitdist));
    58.             }
    59.         }
    60.     }
    61. }
    62.  
     
  2. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Bump.. Anyone plz?
     
  3. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    Hey, i heaven't done anything with NavMeshAgent but have you tried to change the "Angular Speed" in your NavMeshAgent Component ?

    In the manuel they say: "Angular Speed - Maximum speed of rotation (degrees per second)."
    http://docs.unity3d.com/Manual/class-NavMeshAgent.html

    -lg Alex ;)
     
  4. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi m8. Yap i try and goes a bit better but not perfect.. it just keep doing the curve a bit instead of just turn and run back... For me the problem only could be with NavMesh or Character Animations but i believe thats a NavMesh Problem... Appreciate
     
    Last edited: Jan 19, 2015
  5. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    How about that?
    Call that when you get a new Position by Clicking the Mouse (Overwrite your old speed system!!)

    Sytax not Tested!
    Code (CSharp):
    1. void changeSpeed()
    2. {
    3.    float speedMultiplyer = 1.0f - 0.9f * Vector3.Angle (transform.forward, agent.steeringTarget - transform.position) / 180.0f;
    4.    agent.speed = moveSpeed * speedMultiplyer;
    5. }
    6.  
    -lg Alex ;)
     
  6. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    I increase the aceleration in NavMesh and it seems to work now but i will test ur lines ;) Ty M8
     
    Shiikarii likes this.