Search Unity

Dota movement

Discussion in 'Scripting' started by StefanB95, Sep 2, 2011.

  1. StefanB95

    StefanB95

    Joined:
    Mar 14, 2011
    Posts:
    97
    Helo guys so i'm not very good at scripting (not good at all) and i wanna do movement like dota where you click at a point so that the character looks at that point and walks till there so if anyone could help me somehow that would be great thx
     
  2. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,729
    There are some click to move scripts here: http://www.unifycommunity.com/wiki/index.php?title=Scripts

    Here's my current click to move script:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [RequireComponent (typeof(CharacterController))]
    6. public class PlayerMovement : MonoBehaviour {
    7.  
    8.     // The animation component of the player
    9.     public Animation animation;
    10.  
    11.     // The walk, run and rotate speeds of the player
    12.     public float walkSpeed = 1;
    13.     public float runSpeed = 3;
    14.     public float rotateSpeed = 250;
    15.  
    16.     // Layermask used for letting the raycast ignore unwanted layers
    17.     public LayerMask layerMask;
    18.  
    19.     // Private variables
    20.     private CharacterController controller;
    21.     private float speed;
    22.     private Camera camera;
    23.     private Vector3 targetPosition;
    24.     private bool run = false;
    25.  
    26.     public void Awake() {
    27.         controller = GetComponent<CharacterController>();
    28.     }
    29.  
    30.     public void Start () {
    31.         // Find our camera
    32.         camera = gameObject.GetComponentInChildren<Camera>();
    33.  
    34.         // Initialize the position for our rotation
    35.         targetPosition = transform.position;
    36.  
    37.         // Initialize our speed
    38.         speed = runSpeed;
    39.  
    40.         // Make our run animation go at 150% for extra sass
    41.         animation["Run"].speed = 1.5f;
    42.     }
    43.  
    44.     void Update() {
    45.         if(Input.GetButtonUp("Toggle Inventory")) {
    46.             Messenger.Broadcast("ToggleInventory");
    47.         }
    48.  
    49.         if(Input.GetButtonUp("Toggle Character Window")) {
    50.             Messenger.Broadcast("ToggleCharacterWindow");
    51.         }
    52.  
    53.         if(Input.GetButtonDown("Run")) {
    54.             run = !run;
    55.         }
    56.  
    57.         if (run) {
    58.             speed = runSpeed;
    59.         }
    60.         else {
    61.             speed = walkSpeed;
    62.         }
    63.  
    64.         // If the left mouse button is down shoot rays from our camera to the position of our cursor
    65.         if (Input.GetMouseButton(0)) {
    66.             RaycastHit hit;
    67.             if (Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit, 1000, layerMask)) {
    68.                 targetPosition = hit.point;
    69.                 //Debug.DrawLine(camera.transform.position, targetPosition, Color.red);
    70.             }
    71.         }
    72.  
    73.         // If we've reached our destination, stop running
    74.         if (Vector3.Distance(transform.position, targetPosition) < 0.1f) {
    75.             speed = 0;
    76.         }
    77.  
    78.         // Move and rotate towards our destination
    79.         Rotate();
    80.         Move();
    81.  
    82.         Vector3 movement = new Vector3 (controller.velocity.x, 0.0f, controller.velocity.z);
    83.  
    84.         // Go between animations depending on our movement speed
    85.         if (movement.magnitude < 0.5f) {
    86.             animation.CrossFade ("Idle");
    87.         }
    88.         else {
    89.             if (movement.magnitude < 2) {
    90.                 animation.CrossFade ("Walk");
    91.             }
    92.             else {
    93.                 animation.CrossFade ("Run");
    94.             }
    95.         }
    96.     }
    97.  
    98.     // Move our player forwards
    99.     void Move() {
    100.         Vector3 forward = transform.TransformDirection(Vector3.forward);
    101.         controller.SimpleMove(forward * speed);
    102.     }
    103.  
    104.     // Rotate our player towards the cursor
    105.     void Rotate() {
    106.         if (targetPosition != transform.position) {
    107.             Quaternion rot = Quaternion.LookRotation(targetPosition - transform.position);
    108.             float rotationX = rot.eulerAngles.y;
    109.             Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
    110.             transform.localRotation = Quaternion.Slerp(transform.localRotation, xQuaternion, Time.deltaTime * rotateSpeed);
    111.         }
    112.     }
    113. }
    114.  
    It's rough and a work in progress, but it works fairly well.

    I'm using the Viking from the MMO Camera project on the Asset store as my player model currently.
     
  3. StefanB95

    StefanB95

    Joined:
    Mar 14, 2011
    Posts:
    97
    Guys pls help us we tryed this script it didnt work we got a nother one
    but the problem is that it goes on the click position but he faces somewhere els he looks down
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    When posting code, please post it in CODE tags....

    Your answer is a two part one. First, what happens when you click on something other than the ground? You will not register a click. My suggestion to fix this is to use LayerMasks, put all of your ground, or what you can click on into it's own layer, and call it "Ground." We then use a layer mask to define what we can click on. When we do our physics checks to see where we clicked and check it against that mask. Anything that we click on is ignored in favor of the ground.

    The second part is a simple Vector3.Cross and reorient that will get your character face up again. What is happening, is that you click on a destination, say it's below your character, you LookAt it, and your player now faces toward. Move forward and it tries to move into the ground.

    To correct this, we use a Vector3.Cross in the upward direction, from the right direction, to get a new forward direction. Add that to the position, and look at that.

    Code (csharp):
    1.  
    2. var speed : float = 6.0;
    3. var gravity : float = 20.0;
    4. var groundMask : LayerMask;
    5. private var destination : Vector3;
    6. private var iClicked=false;
    7.  
    8. function Start() {
    9.     destination = transform.position;
    10.     StartCoroutine(myCoroutine());
    11. }
    12.  
    13. function Update() {
    14.     if (Input.GetMouseButtonDown(0)){ // if the left mouse button is clicked
    15.         var hit: RaycastHit;
    16.         //cast a ray through the current mouse position from the camera
    17.         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    18.         if (Physics.Raycast (ray, hit, Mathf.Infinity, groundMask)) { //if the ray hits the ground
    19.             destination = hit.point;
    20.             iClicked=true;
    21.         }
    22.     }
    23. }
    24.  
    25. function myCoroutine(){
    26.     var controller : CharacterController = GetComponent(CharacterController);
    27.  
    28.     while(true){
    29.         if(iClicked){ // if we click a mouse, look at the point we clicked
    30.             transform.LookAt(destination);
    31.             var cross=Vector3.Cross(Vector3.up, transform.right);
    32.             transform.LookAt(transform.position + cross);
    33.             iClicked=false;
    34.         }
    35.  
    36.         var moveDirection=Vector3.zero;
    37.         // see if our destination is in front of us
    38.         var z=transform.InverseTransformPoint(destination).z;
    39.         if(z>0.2){
    40.             // if it is, see if we are on the ground
    41.             if (controller.isGrounded) {
    42.                 // if we are, move
    43.                 moveDirection = transform.forward;
    44.             }
    45.             // Apply gravity
    46.             moveDirection.y -= gravity * Time.deltaTime;
    47.             controller.Move(moveDirection * speed * Time.deltaTime);
    48.         }
    49.         yield;
    50.     }
    51. }
    52.