Scripts:
1> Camera 视角跟随(offset)
2> WSAD (方向键)控制角色移动
3> Click to shoot 单击发射子弹
4> Animation_Loop 动画循环播放
************************************************************************************************************************************
一:效果实现
二:Scripts
1> Camera 视角跟随(offset)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollowTarget : MonoBehaviour {
public Transform Succubus;
public Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - Succubus.position;
}
// Update is called once per frame
void Update () {
transform.position = offset + Succubus.position;
}
}
解析:
在Start()函数内部给offset初始化偏移量坐标,相机的坐标-人物的坐标;
然后在Update()更新函数中不断对相机的位置坐标进行更新(人物移动后的坐标+偏移量),
人物的移动可以另外附加脚本进行操作,这里会获取即时的人物坐标来对相机的位置进行赋值。
额外解法:
可以直接将相机物件拖到人物物件下即可(将其作为人物物件的子物件)
2> WSAD (方向键)控制角色移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BirdMovement : MonoBehaviour {
public float speed_wsad = 20;
public float speed_mouse = 20;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
float d = Input.GetAxis("Mouse ScrollWheel");
d = d * speed_mouse;
this.transform.Translate(new Vector3(h, v, d) * Time.deltaTime * speed_wsad);
}
}
解析:
这里的WSAD和键盘的上下左右有一致的效果,这里不用去监测WSAD按键的按下,
可以通过另外一种方式来进行移动操作 Input.GetAxis("Horizontal");:
3> Click to shoot 单击发射子弹
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BirdMovement : MonoBehaviour {
public float speed = 20;
public GameObject bullet;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
//GameObject b = GameObject.Instantiate(this.gameObject, transform.position, transform.rotation); //暴力分裂效果 transform.position
GameObject b = GameObject.Instantiate(bullet, new Vector3(transform.position.x, transform.position.y + 2,transform.position.z), transform.rotation);
Rigidbody rgd = b.GetComponent<Rigidbody>();
rgd.velocity = this.transform.forward * speed;
}
}
}
解析:
获取子弹bullet对象,在鼠标左键按下的时候实例化该对象,并为对象赋予forward方向初速度;
4> Animation_Loop 动画循环播放
(选中动画 -> Wrap Mode -> Loop)