美好的一天!
我需要三种方式来制作此脚本。
1)运动(当前有效) 2)根据运动方向自动旋转(LookAt) 3)使用附加到角色上的动画。
在四处查看后,找不到如何为这些要求集创建此特定脚本。
请帮助!
谢谢。
-PlayerMotor.cs- 使用UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMotor : MonoBehaviour
{
private Vector3 _movVelocity = Vector3.zero;
private Rigidbody rb;
// Use this for initialization
private void Start()
{
// Get the rigidbody component attached to the player
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
PerformMovement();
}
public void Move(Vector3 _velocity)
{
// Set the zero velocity to the active velocity
_movVelocity = _velocity;
}
private void PerformMovement()
{
// Move when the velocity is not zero as per the PlayerController
if (_movVelocity != Vector3.zero)
rb.MovePosition(rb.position + _movVelocity * Time.fixedDeltaTime);
}
// private void Rotation()
// { // Changes direction
//
//
// }
}
-Player Controller.cs-
using CnControls;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float defaultSpeed = 5f;
public float infectedSpeed = 7f;
public float humanSpeed1 = 6f;
public float humanSpeed2 = 7f;
public float humanSpeed3 = 8f;
private PlayerMotor motor;
// Use this for initialization
private void Start()
{
motor = GetComponent<PlayerMotor>();
}
private void Update()
{
// Axis are inverted due to orientation
// Define the axis as per the joystick input
var _xMov = CnInputManager.GetAxis("Vertical");
var _zMov = CnInputManager.GetAxis("Horizontal");
// Debug.Log("X = " + _xMov + " || " + "Z = " + _zMov);
// Set position values as per the joystick input
var _movHorizontal = transform.forward * _xMov;
var _movVertical = transform.right * _zMov;
// Sets speed as per the tag
if (tag.Contains("Zombi"))
{
defaultSpeed = infectedSpeed;
}
else defaultSpeed = humanSpeed1;
// Sets current speed
var _velocity = (_movHorizontal + _movVertical).normalized * defaultSpeed;
// Moves the character using the velocity set
motor.Move(_velocity);
}
}