今天有一个需求就是要做一个箭头 ,要求是当游戏对象在摄像机的左边的时候箭头向右指 ,反之箭头向左。
首先进行创建一个场景,然后在进行创建一个画布在画布下面进行创建两个图片,然后把画布作为摄像机的子物体
在在摄像机上进行添加上一个脚本(CameraMoveAndRotate )用来进行控制相机得旋转,然后在给摄像机进行添加一个脚本进行控制箭头。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMoveAndRotate : MonoBehaviour {
private void Update()
{
float h = Input.GetAxis(“Horizontal”);
float v = Input.GetAxis(“Vertical”);
if (h != 0 || v != 0)
{
Move(h,v);
}
float rx = Input.GetAxis(“Mouse X”);
float yx = Input.GetAxis(“Mouse Y”);
if (rx!= 0 || yx != 0)
{
RotateT(rx, yx);
}
}
void Move(float x,float z)
{
Vector3 dir = new Vector3(x,0,z);
transform.Translate(dirTime.deltaTime5);
}
void RotateT(float x,float y)