Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class ClickToMove : MonoBehaviour {
- public float speed;
- public CharacterController controller;
- public Animator Animator;
- private Vector3 position;
- void locatePosition()
- {
- RaycastHit hit;
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- if(Physics.Raycast(ray, out hit, 1000))
- {
- position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
- }
- }
- void moveToPosition()
- {
- if(Vector3.Distance(transform.position, position)>1)
- {
- Quaternion newRotation = Quaternion.LookRotation(position-transform.position);
- newRotation.x = 0f ;
- newRotation.z = 0f ;
- transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
- controller.SimpleMove(transform.forward * speed);
- }
- }
- // Use this for initialization
- void Start ()
- {
- }
- // Update is called once per frame
- void Update ()
- {
- if(Input.GetMouseButton(0)){
- locatePosition();
- moveToPosition();
- }
- bool andando = Vector3.Distance (transform.position, position) > 1f;
- Animator.SetBool("Andar", andando);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement