我制作了带有触摸位置的玩家脚本和翻转播放器的方向运行方向是我这样做但只是从0翻转。如果在x = -5并且我触摸进入x = 2玩家不要翻转但是当x = 0时是翻转。有人可以告诉我如何做到这一点,我喜欢用弗洛尔制作,但我的球员只能在yAxis方向上移动一个弗洛尔但是当我移除yAxis时它会四处移动我喜欢像这个游戏一样移动Game < / p>
if (gameObject.transform.position.x > 0 && faceRight)
{
Flip();
}
else if (gameObject.transform.position.x < 0 && !faceRight)
{
Flip();
}
&#13;
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
private bool flag = false;
private Vector3 endPoint;
public float duration;
private float yAxis;
private bool Run = false;
private Animator anim;
private bool faceRight = true;
void Start()
{
anim = GetComponent<Animator>();
//save the y axis value of gameobject
yAxis = gameObject.transform.position.y;
}
// Update is called once per frame
void Update()
{
//check if the screen is touched / clicked
if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0)))
{
//declare a variable of RaycastHit struct
RaycastHit hit;
//Create a Ray on the tapped / clicked position
Ray ray;
//for unity editor
#if UNITY_EDITOR
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//for touch device
#elif (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8)
ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
#endif
//Check if the ray hits any collider
if (Physics.Raycast(ray, out hit))
{
//set a flag to indicate to move the gameobject
flag = true;
//save the click / tap position
endPoint = hit.point;
//as we do not want to change the y axis value based on touch position, reset it to original y axis value
endPoint.y = yAxis;
Debug.Log(endPoint);
}
}
//check if the flag for movement is true and the current gameobject position is not same as the clicked / tapped position
if (flag && !Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude))
{ //&& !(V3Equal(transform.position, endPoint))){
//move the gameobject to the desired position
gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, endPoint, 1 / (duration * (Vector3.Distance(gameObject.transform.position, endPoint))));
Run = true;
anim.SetBool("Run", Run);
if (gameObject.transform.position.x > 0 && !faceRight)
{
Flip();
}
else if (gameObject.transform.position.x < 0 && faceRight)
{
Flip();
}
}
//set the movement indicator flag to false if the endPoint and current gameobject position are equal
else if (flag && Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude))
{
flag = false;
Run = false;
anim.SetBool("Run", Run);
Debug.Log("I am here");
}
}
void Flip()
{
faceRight = !faceRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
&#13;
答案 0 :(得分:0)
我的anwer与你的代码不同但是如果你想在左边跑时翻转角色(我假设默认为右),你可以简单地检查一下
如果角色的速度&gt; 0然后缩放1并且如果字符的速度<1。 0然后缩放-1
这将面对你去的旋转角色,这将比原始代码容易,但如果你想要实现别的东西,请告诉我,以便我可以纠正我对你的需求的答案:)
答案 1 :(得分:0)
翻转角色似乎是Animator
应该处理的事情。我看到你已经在使用Animator
,所以你应该做类似的事情:
anim.SetInteger("direction", 1)
甚至:
anim.SetFloat("position", transform.position.x)
并配置动画师进行相应操作,创建动画状态,使角色面向左右(使用gameObject的变换scale.x
1
和-1
或rotation.y
{ {1}}和0
)以及响应您决定观看的事件的过渡。