废话不多说直接看效果:
这是在player里面物体的脚本:
代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerSword : MonoBehaviour
{
public GameObject player; // 播放器对象
Vector3 position;
// Start is called before the first frame update
void Start()
{
position = transform.localPosition;
}
void IsFacingRight(bool isFacingRight)
{
if (isFacingRight)
{
transform.localPosition = position;
transform.localScale = new Vector3(1, 1, 1);
}
else
{
transform.localPosition = new Vector3(-position.x, position.y, position.z);
transform.localScale = new Vector3(-1, 1, 1);
}
}
// Update is called once per frame
void Update()
{
IsFacingRight(player.transform.localScale.x > 0);
}
}
Player调用脚本:
左右靠传值true/false改变,你们根据自己的需求变通。
gameObject.BroadcastMessage("物体里面的子物体的方法",自己需要传的值)
抽帧截屏很辛苦的,觉得有用来个点赞+收藏吧,谢谢!