我有一个有刚毅的球员,我想让它沿着路线前进。我使用过MoveTowards,但是它似乎也不起作用。我想使用MovePosition来增加磨削时的力量。我注释了一段可能有效的代码,但在这方面我发现了一个错误,请检查//ADDFORCE行。
这是我的代码:
public GameObject[] waypoints;
public float grindSpeed;
public float turnSpeed;
public int currentWaypoint;
private Animator anim;
private Rigidbody rb;
public bool isGrinding = false;
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
MoveAlongWaypoints();
if(isGrinding)
anim.SetBool ("isOnGrinding", true);
else if(!isGrinding)
anim.SetBool("isOnGrinding", false);
}
void MoveAlongWaypoints()
{
if(isGrinding)
{
//TRANSLATE
transform.position = Vector3.MoveTowards(transform.position, waypoints[currentWaypoint].transform.position, grindSpeed * Time.deltaTime);
//ADDFORCE
/*
Vector3 movePosition = transform.position;
movePosition = Mathf.MoveTowards(transform.position, waypoints[currentWaypoint].transform.position * grindSpeed * Time.deltaTime);
rb.MovePosition(movePosition);
*/
//ROTATE
var rotation = Quaternion.LookRotation(waypoints[currentWaypoint].transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, turnSpeed * Time.deltaTime);
if(Mathf.Abs(transform.position.x - waypoints[currentWaypoint].transform.position.x) < 1
&& (Mathf.Abs (transform.position.y - waypoints[currentWaypoint].transform.position.y) < 1)
&& (Mathf.Abs (transform.position.z - waypoints[currentWaypoint].transform.position.z) < 1))
{
//rb.useGravity = false;
currentWaypoint++;
}
}
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "GrindWayPoint")
{
Debug.Log ("Waypoint!!");
isGrinding = true;
}
}
发布于 2018-01-26 12:11:19
看起来你的代码应该有效..。颠簸。不过,我确实有一些建议。
1)在使用刚体时尝试在FixedUpdate中工作。
2)使用movePosition移动刚体,而不是设置位置。
以上2个提示将导致您的游戏抖动较少。如果您使用fixedUpdate移动刚体,它可以通过对撞机移动,而不会在物理步骤中被检测到。
3) anim.SetBool ("isOnGrinding",isGrinding);比您在fixedUpdate中已有的代码少3行。
4)向量3.距离()也比计算vector3的所有三轴之间的偏移量短。
5)使用moveTowards时,不要将targetPosition乘以浮点数,这将给您一个完全不同的位置。如果您想逐步移动它,请使用Vector3.Lerp,或者先计算偏移量。
尝试提供更多的信息,这似乎是可行的,但我真的不知道任何关于你的设置。你的方向点上有对撞机吗?或者有路径点的gameObject有对撞机吗?
发布于 2018-01-30 04:43:42
您的Vector3. Rb.MoveTowards和Rb.MoveTowards将同样工作。但是,您可以使用Vector3.Distance()代替
if(Mathf.Abs(transform.position.x - waypoints[currentWaypoint].transform.position.x) < 1
&& (Mathf.Abs (transform.position.y - waypoints[currentWaypoint].transform.position.y) < 1)
&& (Mathf.Abs (transform.position.z - waypoints[currentWaypoint].transform.position.z) < 1))
{
//rb.useGravity = false;
currentWaypoint++;
}
你可以把它当作
Vector3.Distance(transform.position,waypoints[currentwaypoint].transfrom.positon)
它会返回浮子,你可以用它做一个条件。此外,您也没有考虑trasnform.forward在您的运动。玩家不能向前移动。你应该看看这个链接。
https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html
https://gamedev.stackexchange.com/questions/153612
复制相似问题