system
1
I’m working on a simple twin stick shooter control script. All is working fine… when the character is moving, if you let go of the left stick, the right stick will no longer rotate the sprite.
using UnityEngine;
using System.Collections;
public class player_movement : MonoBehaviour
{
public int playerNumber;
public Vector2 rightStick;
private float angularVelocity;
private float radialDeadZone;
public Vector2 direction;
private float currentRotation;
public float speed;
// Use this for initialization
void Start ()
{
rightStick = new Vector2(0,0);
angularVelocity = 12.0f;
radialDeadZone = 0.1f;
}
// Update is called once per frame
void FixedUpdate ()
{
//Debug.Log("RSx " + rightStick.x + " RSy " + rightStick.y + " LSy " + Input.GetAxis("LStickY_"+playerNumber) + " RSx " + Input.GetAxis("LStickX_"+playerNumber));
float moveHorizontal = Input.GetAxis ("LStickX_"+playerNumber)*speed;
float moveVertical = Input.GetAxis ("LStickY_"+playerNumber)*speed;
rigidbody2D.velocity = new Vector2(moveHorizontal, moveVertical);
rightStick = new Vector2(Input.GetAxis("RStickY_"+playerNumber), Input.GetAxis("RStickX_"+playerNumber));
direction = new Vector3(rightStick.x, rightStick.y);
if(direction.magnitude > radialDeadZone)
{
Debug.Log(Mathf.Atan2(direction.x,direction.y)* Mathf.Rad2Deg);
currentRotation = Mathf.Atan2(direction.x,direction.y)* Mathf.Rad2Deg;
rigidbody2D.rotation = Mathf.Lerp(transform.rotation.x, currentRotation, Time.deltaTime* 360);
}
}
}
Am I missing something to do with rigidbody2d? I’m feeding rigidbody2d.rotation a float and rigidbody2d.velocity a vector2.