#Hey I have a question here it is I can t
1 messages · Page 1 of 1 (latest)
using UnityEngine;
using System.Collections;
using Rewired;
[RequireComponent(typeof(Rigidbody2D))]
public class MyCharacter : MonoBehaviour {
// The Rewired player id of this character
public int playerId = 0;
// The movement speed of this character
public float moveSpeed = 3.0f;
public bool m_FacingRight = true;
private Player player; // The Rewired Player
private Rigidbody2D rb;
private Vector3 moveVector;
private Animator animator;
private int playerDirection = 1;
void Awake() {
// Get the Rewired Player object for this player and keep it for the duration of the character's lifetime
player = ReInput.players.GetPlayer(playerId);
// Get the character controller
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
void Update () {
GetInput();
ProcessInput();
}
private void GetInput() {
// Get the input from the Rewired Player. All controllers that the Player owns will contribute, so it doesn't matter
// whether the input is coming from a joystick, the keyboard, mouse, or a custom controller.
moveVector.x = player.GetAxis("Move Horizontal"); // get input by name or action id
moveVector.y = player.GetAxis("Move Vertical");
}
private void ProcessInput() {
// Process movement
if(moveVector.x != 0.0f || moveVector.y != 0.0f) {
//rb.MovePosition(moveVector * moveSpeed * Time.deltaTime);
var movement = new Vector2(moveVector.x, moveVector.y);
rb.velocity = (movement * moveSpeed);
animator.SetFloat("Horizontal Speed", rb.velocity.x);
animator.SetFloat("Vertical Speed", rb.velocity.y);