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
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): using UnityEngine; using System.Collections; [RequireComponent (typeof(CharacterController))] public class PlayerMovement : MonoBehaviour { // The animation component of the player public Animation animation; // The walk, run and rotate speeds of the player public float walkSpeed = 1; public float runSpeed = 3; public float rotateSpeed = 250; // Layermask used for letting the raycast ignore unwanted layers public LayerMask layerMask; // Private variables private CharacterController controller; private float speed; private Camera camera; private Vector3 targetPosition; private bool run = false; public void Awake() { controller = GetComponent<CharacterController>(); } public void Start () { // Find our camera camera = gameObject.GetComponentInChildren<Camera>(); // Initialize the position for our rotation targetPosition = transform.position; // Initialize our speed speed = runSpeed; // Make our run animation go at 150% for extra sass animation["Run"].speed = 1.5f; } void Update() { if(Input.GetButtonUp("Toggle Inventory")) { Messenger.Broadcast("ToggleInventory"); } if(Input.GetButtonUp("Toggle Character Window")) { Messenger.Broadcast("ToggleCharacterWindow"); } if(Input.GetButtonDown("Run")) { run = !run; } if (run) { speed = runSpeed; } else { speed = walkSpeed; } // If the left mouse button is down shoot rays from our camera to the position of our cursor if (Input.GetMouseButton(0)) { RaycastHit hit; if (Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit, 1000, layerMask)) { targetPosition = hit.point; //Debug.DrawLine(camera.transform.position, targetPosition, Color.red); } } // If we've reached our destination, stop running if (Vector3.Distance(transform.position, targetPosition) < 0.1f) { speed = 0; } // Move and rotate towards our destination Rotate(); Move(); Vector3 movement = new Vector3 (controller.velocity.x, 0.0f, controller.velocity.z); // Go between animations depending on our movement speed if (movement.magnitude < 0.5f) { animation.CrossFade ("Idle"); } else { if (movement.magnitude < 2) { animation.CrossFade ("Walk"); } else { animation.CrossFade ("Run"); } } } // Move our player forwards void Move() { Vector3 forward = transform.TransformDirection(Vector3.forward); controller.SimpleMove(forward * speed); } // Rotate our player towards the cursor void Rotate() { if (targetPosition != transform.position) { Quaternion rot = Quaternion.LookRotation(targetPosition - transform.position); float rotationX = rot.eulerAngles.y; Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up); transform.localRotation = Quaternion.Slerp(transform.localRotation, xQuaternion, Time.deltaTime * rotateSpeed); } } } 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.
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
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): var speed : float = 6.0; var gravity : float = 20.0; var groundMask : LayerMask; private var destination : Vector3; private var iClicked=false; function Start() { destination = transform.position; StartCoroutine(myCoroutine()); } function Update() { if (Input.GetMouseButtonDown(0)){ // if the left mouse button is clicked var hit: RaycastHit; //cast a ray through the current mouse position from the camera var ray = Camera.main.ScreenPointToRay (Input.mousePosition); if (Physics.Raycast (ray, hit, Mathf.Infinity, groundMask)) { //if the ray hits the ground destination = hit.point; iClicked=true; } } } function myCoroutine(){ var controller : CharacterController = GetComponent(CharacterController); while(true){ if(iClicked){ // if we click a mouse, look at the point we clicked transform.LookAt(destination); var cross=Vector3.Cross(Vector3.up, transform.right); transform.LookAt(transform.position + cross); iClicked=false; } var moveDirection=Vector3.zero; // see if our destination is in front of us var z=transform.InverseTransformPoint(destination).z; if(z>0.2){ // if it is, see if we are on the ground if (controller.isGrounded) { // if we are, move moveDirection = transform.forward; } // Apply gravity moveDirection.y -= gravity * Time.deltaTime; controller.Move(moveDirection * speed * Time.deltaTime); } yield; } }