- Home /
Right Stick Aiming Problem (Dual Stick Shooter)
Hello, I'm currently building a dual stick shooter using a PS4 controller and after getting the right analogue stick to work properly ("3rd-axis" and "6th-axis" weirdly if anyone wants to know), I'm now having difficulties with the movement of my turret.
When the input on the y axis is positive, the aiming is fine, however when I flip the x rotation when the input is negative, it does it constantly. I'm guessing I should run some sort of check with the current axis to stop it from doing this, but I can't seem to figure it out. Here's the script anyway:
using UnityEngine;
using System.Collections;
public class PlayerAiming : MonoBehaviour
{
// Update is called once per frame
void Update ()
{
// We are going to read the input every frame
Vector3 vNewInput = new Vector3(Input.GetAxis("RightStickX"), Input.GetAxis("RightStickY"), 0.0f);
// Only do work if meaningful
if(vNewInput.sqrMagnitude < 0.1f)
{
return;
}
// Set our rotation to represent the input
vNewInput.Normalize();
float fHeading = Vector3.Dot(Vector3.right, vNewInput);
Vector3 vNewRotation = transform.rotation.eulerAngles;
vNewRotation.z = fHeading * 90.0f;
// Adjust our rotation if we're on the bottom half of the input circle
if(vNewInput.y > 0.0f)
{
Debug.Log(new Vector2(Input.GetAxis("RightStickX"), Input.GetAxis("RightStickY"))); //report value of right thumbstick
vNewRotation.x = 180.0f - vNewRotation.x;
}
// Apply the transform to the object
transform.rotation = Quaternion.Euler(vNewRotation);
}
}
Any Help would be appreciated thanks.
Answer by Shadowed_Phoenix · Feb 05, 2014 at 01:31 PM
Managed to figure it out using some of the code from this and this
Here is the modified code if anyone is interested:
using UnityEngine;
using System.Collections;
public class PlayerAiming : MonoBehaviour
{
// Update is called once per frame
void Update ()
{
// We are going to read the input every frame
Vector3 vNewInput = new Vector3(Input.GetAxis("RightStickX"), Input.GetAxis("RightStickY"), 0.0f);
// Only do work if meaningful
if(vNewInput.sqrMagnitude < 0.1f)
{
return;
}
// Apply the transform to the object
var angle = Mathf.Atan2(Input.GetAxis("RightStickX"), Input.GetAxis("RightStickY")) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(angle, 0, 0);
}
}
Answer by rie studios · Nov 05, 2014 at 07:19 PM
I dont know how anyone hasnt thanked you yet but this is immensely helpful!
Answer by Okabim · Jan 19, 2021 at 09:29 PM
Thank you so much for this! It really helped me out a lot.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
PS3 Analogue Stick Axis Values 1 Answer
Help regarding World-Local Transfrom Rotation? 1 Answer
Rotating object with a mouse movement 0 Answers