游戏开发学习笔记(五)人物的移动及相机的跟随

思路:

将摇杆移动的坐标传给人物

给人物添加状态机及设置状态转换

相机的坐标跟随人物的坐标


将摇杆移动的坐标传给人物,给人物添加状态机及设置状态转换

给人物添加Character Control和Animator组件,添加PlayerControl脚本,编辑脚本
public class PlayerControl : MonoBehaviour {

	public float speed = 4f;

	private CharacterController PlayerCC;
	private Animator PlayerAnim;

	void Awake(){
		PlayerCC = this.GetComponent ();
		PlayerAnim = this.GetComponent ();

	}

	void Update () {
		if (JoyStick._instance.isPress) {
			PlayerAnim.SetBool ("Idle_Run", JoyStick._instance.isPress);
			Vector3 Dir = new Vector3 (transform.position.x + JoyStick._instance.touchPos.x, transform.position.y, transform.position.z + JoyStick._instance.touchPos.y);
			transform.LookAt (Dir);
			PlayerCC.SimpleMove (transform.forward * speed);
			PlayerCC.SimpleMove (transform.forward * speed);
		} else {
			PlayerAnim.SetBool ("Idle_Run", JoyStick._instance.isPress);
		}
		
	}
}

相机的坐标跟随人物的坐标

给主摄像机MainCamera添加脚本CameraFollow脚本,编辑脚本

public class CameraFollow : MonoBehaviour {  
  
    private Transform player;  
    private Vector3 offsetpos;  
  
    void Start () {  
        player = GameObject.Find ("Player").transform;  
        offsetpos = transform.position - player.position;  
    }  
  
    void Update () {  
        transform.position = player.position + offsetpos;  
    }  
}  


你可能感兴趣的:(《荣耀》)