人物鼠标点击控制以及摄像机跟随

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class playerController : MonoBehaviour {
    public Texture mouse;
    public GameObject effectClick;
    private float distance;
    public float speed = 3f;
    private bool moveTo = false;
    private CharacterController characterCon;
    private Vector3 targetPosition=Vector3.zero;
    private Animation anim;
// Use this for initialization
void Start () {
        anim = GetComponent<Animation>();
        characterCon = GetComponent<CharacterController>();
}

// Update is called once per frame
void Update () {
        if (moveTo)
        {      
            distance = Vector3.Distance(transform.position,targetPosition);
            if (distance > 0.3f&&distance<3f)
            {
                characterCon.SimpleMove(transform.forward * speed ); 这块是通过添加角色控制器,然后让物体移动。

                anim.CrossFade("Run");
            }
            else
            {
                moveTo = false;
            }
        }
        else
        {
            anim.CrossFade("Idle");
        }
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if (Physics.Raycast(ray, out hitInfo))
            {
                if (hitInfo.collider.tag == "Ground")
                {
                    LookAt(hitInfo.point);
                    moveTo = true;
                }
            }
        }  
    }
       
    public void LookAt(Vector3 point)
    {
        targetPosition = new Vector3(point.x,transform.position.y,point.z);
        transform.LookAt(targetPosition);
        Instantiate(effectClick, new Vector3(targetPosition.x,targetPosition.y+0.2f,targetPosition.z), Quaternion.identity);  
    }
   void OnGUI()
    {
        Vector2 mspos = Input.mousePosition;

        GUI.DrawTexture(new Rect(mspos.x, Screen.height - mspos.y,0,0),mouse);这块是用于产生鼠标指针的。

    }

}


摄像机跟随脚本

using UnityEngine;
using System.Collections;


public class FollowPlayer : MonoBehaviour
{


    private Transform player;
    private Vector3 offsetPosition;//位置偏移
    private bool isRotating = false;




    public float distance = 0;
    public float scrollSpeed = 10;
    public float rotateSpeed = 2;


    // Use this for initialization
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
        transform.LookAt(player.position);
        offsetPosition = transform.position - player.position;
    }


    // Update is called once per frame
    void Update()
    {
        transform.position = offsetPosition + player.position;
        transform.LookAt(player.position);
        //处理视野的旋转
        RotateView();
        //处理视野的拉近和拉远效果
        ScrollView();
    }


    void ScrollView()
    {
        //print(Input.GetAxis("Mouse ScrollWheel"));//向后 返回负值 (拉近视野) 向前滑动 返回正值(拉远视野)
        distance = offsetPosition.magnitude;
        distance += Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
        distance = Mathf.Clamp(distance, 2, 18);
        offsetPosition = offsetPosition.normalized * distance;
    }


    void RotateView()
    {
        //Input.GetAxis("Mouse X");//得到鼠标在水平方向的滑动
        //Input.GetAxis("Mouse Y");//得到鼠标在垂直方向的滑动
        if (Input.GetMouseButtonDown(1))
        {
            isRotating = true;
        }
        if (Input.GetMouseButtonUp(1))
        {
            isRotating = false;
        }


        if (isRotating)
        {
           
            transform.RotateAround(player.position, player.up, rotateSpeed * Input.GetAxis("Mouse X"));
            Vector3 originalPos = transform.position;
            Quaternion originalRotation = transform.rotation;
            transform.RotateAround(player.position, transform.right, -rotateSpeed * Input.GetAxis("Mouse Y"));//影响的属性有两个 一个是position 一个是rotation
           
            float x = transform.eulerAngles.x;
            if (x < 10 || x > 80)
            {//当超出范围之后,我们将属性归位原来的,就是让旋转无效 
                transform.position = originalPos;
                transform.rotation = originalRotation;
            }

        }
        offsetPosition = transform.position - player.position;
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值