如何使斧头投掷和回击系统统一起来

  • 本文关键字:系统 一起来 何使斧 c
  • 更新时间 :
  • 英文 :


伙计们,

所以,我试图在单位里制作一个类似战神的游戏,但我不知道如何让斧头动态返回,eustou试图让它毫无问题地返回,但我仍然无法得到满意的结果,我正在使用动画让斧头旋转,当它离玩家足够近时,该脚本停用了Animator和一个四元数。Slerp更正了ax的旋转,这是代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AxeController : MonoBehaviour {
// The axe object
public Rigidbody axe;
// Amount of force to apply when throwing
public float throwForce = 50;
// the target; which is the player's hand.
public Transform target;
// The middle point between the axe and the player's hand, to give it a curve
public Transform curve_point;
public Transform hand;
// Last position of the axe before returning it, to use in the Bezier Quadratic Curve formula
private Vector3 old_pos;
// Is the axe returning? To update the calculations in the Update method
private bool isReturning = false;
// Timer to link to the Bezier formual, Beginnning = 0, End = 1
public float time = 0.0f;
public float RotationSpeed;
public float MinDistance;
public float Distance;
public bool hasAxe = true;
public bool debounce = true;
public float gunDelay;
[Range (-10,10)]
public float TimeReduction;
[Range (0.1f , 100)]
public float VelocidadeDeAjuste;
// Update is called once per frame
void Update () {
Distance = Vector3.Distance(target.position, axe.transform.position);
// When the user/player hits the mouse left button
if (Input.GetButtonUp("Fire1") && hasAxe == true && debounce == true)
{
ThrowAxe();
debounce = false;
Invoke("debouncer" , gunDelay);
}
// When the user/player hits the mouse right button
if (Input.GetButtonUp("Fire1") && hasAxe == false && debounce == true)
{
ReturnAxe();
debounce = false;
Invoke("debouncer" , gunDelay);
}
// If the axe is returning
if(isReturning){
// Returning calcs
// Check if we haven't reached the end point, where time = 1


if(time < 1.0f){
// Update its position by using the Bezier formula based on the current time
axe.position = getBQCPoint(time, old_pos, curve_point.position, target.position);
// Reset its rotation (from current to the targets rotation) with 50 units/s
if (Distance > MinDistance) {
//axe.transform.localEulerAngles += Vector3.forward * Time.deltaTime;
Debug.Log("Nao Esta Distancia");
}
if (Distance <= MinDistance) {
axe.rotation = Quaternion.Slerp(axe.transform.rotation, target.rotation,Time.deltaTime * (time * VelocidadeDeAjuste));
axe.GetComponent<Animator>().enabled = false;
Debug.Log("Em Distancia");
}
else
{
axe.GetComponent<Animator>().enabled = true;
}
// Increase our timer, if you want the axe to return faster, then increase "time" more
// With something like:
// time += Timde.deltaTime * 2;
// It will return as twice as fast
time += Time.deltaTime/TimeReduction;
}else{
// Otherwise, if it is 1 or more, we reached the target so reset
ResetAxe();
}
}
}
// Throw axe
void ThrowAxe(){
// The axe isn't returning
isReturning = false;
hasAxe = false;
// Deatach it form its parent
axe.transform.parent = null;
// Set isKinematic to false, so we can apply force to it
axe.isKinematic = false;
// Add force to the forward axis of the camera
// We used TransformDirection to conver the axis from local to world
axe.AddForce(Camera.main.transform.TransformDirection(Vector3.forward) * throwForce, ForceMode.Impulse);
// Add torque to the axe, to give it much cooler effect (rotating)
axe.AddTorque(axe.transform.TransformDirection(Vector3.forward) * RotationSpeed, ForceMode.Impulse);
axe.transform.GetComponent<AxeWeapon>().Retornando = false;
}
// Return axe
void ReturnAxe(){
// We are returning the axe; so it is in its first point where time = 0
time = 0.0f;
// Save its last position to refer to it in the Bezier formula
old_pos = axe.position;
// Now we are returning the axe, to start the calculations in the Update method
isReturning = true;
// Reset its velocity
axe.velocity = Vector3.zero;
// Set isKinematic to true, so now we control its position directly without being affected by force
axe.isKinematic = true;
axe.AddTorque(axe.transform.TransformDirection(Vector3.forward * -1) * RotationSpeed, ForceMode.Impulse);
axe.transform.GetComponent<AxeWeapon>().Retornando = true;
}
// Reset axe
void ResetAxe(){
// Axe has reached, so it is not returning anymore
isReturning = false;
// Attach back to its parent, in this case it will attach it to the player (or where you attached the script to)
axe.transform.parent = transform;
// Set its position to the target's
axe.position = target.position;
// Set its rotation to the target's
axe.rotation = target.rotation;
hasAxe = true;
axe.transform.GetComponent<AxeWeapon>().Retornando = false;
}
// Bezier Quadratic Curve formula
// Learn more:
// https://en.wikipedia.org/wiki/B%C3%A9zier_curve
Vector3 getBQCPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2){
// "t" is always between 0 and 1, so "u" is other side of t
// If "t" is 1, then "u" is 0
float u = 1 - t;
// "t" square
float tt = t * t;
// "u" square
float uu = u * u;
// this is the formula in one line
// (u^2 * p0) + (2 * u * t * p1) + (t^2 * p2)
Vector3 p = (uu * p0) + (2
* u * t * p1) + (tt * p2);
return p;
}
void debouncer()
{
debounce = true;
}
}

enter code here

我现在有这个:

https://youtu.be/cEYYnGo-4dg

我觉得你把事情搞得太复杂了。你唯一需要的变量是返回位置(玩家的手(、斧头的速度和动画师变量。关于曲线和旋转,你可以用动画来做,因为它会更多地清理代码。对于玩家想要扔斧头的地方,你可以进行光线投射,检查斧头是否指向合适的目标。并确保玩家不能只是添加延迟并根据您的需要进行修改。希望这能有所帮助!:D

最新更新