- Home /
Smoother rotation when following a joystick axis
It took me a long time, but I finally got the camera inside of a cockpit to rotate independently of the ship. (The answer was so friggin simple, I just didn't know to use localRotation instead of rotation) but now the problem is that the camera follows the stick so exactly that it can be disorienting. Is there an effective way to smooth out the motion so that the camera rotates with a slight delay?
I'm guessing it has something to do with clamping rotation values then having the stick apply a rotate value which can then be smoothed instead of having its localRotation match the stick's axis, but I have no idea where to start with that theory. And importantly, the camera needs to automatically re-center itself when the stick axis returns to 0.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShipScript : MonoBehaviour
{
//Ship
public float forwardSpeed;
public float reverseSpeed;
public float pitchSpeed;
public float rollSpeed;
public float yawSpeed;
public float pitchTorque;
public float rollTorque;
public float yawTorque;
public float pitchAxisVal;
public float rollAndYawAxisVal;
public float thrustSpeed;
public float thrustRate;
public float thrustMax;
public float thrustMin;
public float currentPitch;
public float currentYaw;
public float currentRoll;
public Rigidbody ship;
//Camera
public float pitchCamVal;
public float yawCamVal;
public float rollCamVal;
public Transform holder;
public Camera cockpit;
private float cockpitCameraAxis;
private Transform holderRot;
//Shooting
public float fireMain;
public GameObject shotPulse;
public Transform barrelLeft;
public Transform barrelRight;
public float fireRate;
public float nextFireLeft;
public float nextFireRight;
public bool alternate;
private void Start()
{
thrustSpeed = 0.0f;
thrustMax = 30.0f;
thrustMin = 0.0f;
alternate = false;
}
private void Update()
{
fireMain = Input.GetAxisRaw("RightTrigger");
pitchCamVal = Input.GetAxis("LeftV");
yawCamVal = Input.GetAxis("LeftH");
pitchAxisVal = Input.GetAxis("RightV");
rollAndYawAxisVal = Input.GetAxis("RightH");
currentPitch = ship.rotation.x;
currentYaw = ship.rotation.y;
currentRoll = ship.rotation.z;
//Camera
//DO NOT DELETE THE FOLLOWING LINE
holder.localRotation = Quaternion.Euler(currentPitch + (pitchCamVal * 45), currentYaw + (yawCamVal * 90), currentRoll);
//Movement
if (Input.GetKey(KeyCode.Joystick1Button9))
{
ship.AddTorque(transform.right * -pitchAxisVal * pitchTorque);
ship.AddTorque(transform.up * rollAndYawAxisVal * yawTorque);
} else
{
ship.AddTorque(transform.right * -pitchAxisVal * pitchTorque);
ship.AddTorque(transform.forward * -rollAndYawAxisVal * rollTorque);
}
if (Input.GetKeyDown(KeyCode.Joystick1Button0))
{
Debug.Log(holder.transform.rotation.x + " , " + holder.transform.rotation.y + " , " + holder.transform.rotation.z);
Debug.Log(ship.transform.rotation.x + " , " + ship.transform.rotation.y + " , " + ship.transform.rotation.z);
}
//Shooting
if (fireMain == 1f && Time.time > nextFireRight && alternate == true)
{
nextFireLeft = Time.time + fireRate;
Instantiate(shotPulse, barrelLeft.position, barrelLeft.rotation);
alternate = false;
}
if (fireMain == 1f && Time.time > nextFireLeft && alternate == false)
{
nextFireRight = Time.time + fireRate;
Instantiate(shotPulse, barrelRight.position, barrelRight.rotation);
alternate = true;
}
}
private void FixedUpdate()
{
//Movement
ship.transform.Translate(Vector3.forward * thrustSpeed / 25.0f);
if (Input.GetKey(KeyCode.Joystick1Button7) && (thrustSpeed < thrustMax))
{
thrustSpeed = thrustSpeed + thrustRate;
Debug.Log("+: " + thrustSpeed);
}
if (Input.GetKey(KeyCode.Joystick1Button6) && (thrustSpeed > thrustMin))
{
thrustSpeed = thrustSpeed - thrustRate;
Debug.Log("-: " + thrustSpeed);
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how to rotate GameObject towards joystick 1 Answer
Object Rotation around another object with right stick 0 Answers
Cross Platform Joystick Rotation 0 Answers