So funny thing, with the new input system the movement is digital, so it goes immediately to -1 or 1 etc, which makes the rotations due to controls happen instantly. Unless I missed something along the line.
I want to make my own assets later on so I’m not fine tuning this yet.
Thank you for showing this! I thought i had made a mistake somewhere. I have the same thing happening.
Yeah a ramping function for the values needs to be implemented at some point, but I think it will be easier to adjust once I have some assets to play with.
I think https://docs.unity3d.com/ScriptReference/Mathf.SmoothDamp.html is what one would want to use?
Played around with Mathf.SmoothDamp and here’s the result:
Just using it to smooth the input so it doesn’t go from 0-1 instantly.
@tobekko I have been trying to do the same thing, but I can’t wrap my head around how Mathf.SmoothDamp () works. How did you do it? Can you post some code?
Sure, here’s the entire thing:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerControls : MonoBehaviour
{
[SerializeField] float xSpeed = 20f;
[SerializeField] float xRange = 10f;
[SerializeField] float ySpeed = 20f;
[SerializeField] float yRange = 7f;
[SerializeField] float positionPitchFactor = -2f;
[SerializeField] float controlPitchFactor = -10f;
[SerializeField] float positionYawFactor = 2f;
[SerializeField] float controlYawFactor = -10f;
[SerializeField] float smoothTimeX = 0.2f;
[SerializeField] float smoothTimeY = 0.2f;
[SerializeField] InputAction movement;
float yVel = 0.0f;
float xVel = 0.0f;
float yThrow;
float xThrow;
float oldXThrow = 0f;
float oldYThrow = 0f;
// Start is called before the first frame update
void Start()
{
}
private void OnEnable()
{
movement.Enable();
}
private void OnDisable()
{
movement.Disable();
}
// Update is called once per frame
void Update()
{
ProcessTranslation();
ProcessRotation();
}
void ProcessRotation()
{
float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;
float pitchDueToControlThrow = yThrow * controlPitchFactor;
float pitch = pitchDueToPosition + pitchDueToControlThrow;
float yaw = transform.localPosition.x * positionYawFactor;
float roll = xThrow * controlYawFactor;
transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
}
void ProcessTranslation()
{
oldXThrow = xThrow;
xThrow = Mathf.SmoothDamp(oldXThrow, movement.ReadValue<Vector2>().x, ref xVel, smoothTimeX);
oldYThrow = yThrow;
yThrow = Mathf.SmoothDamp(oldYThrow, movement.ReadValue<Vector2>().y, ref yVel, smoothTimeY);
float xOffset = xThrow * Time.deltaTime * xSpeed;
float rawXPos = transform.localPosition.x + xOffset;
float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);
float yOffset = yThrow * Time.deltaTime * ySpeed;
float rawYPos = transform.localPosition.y + yOffset;
float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);
transform.localPosition = new Vector3 (clampedXPos, clampedYPos, transform.localPosition.z);
}
}
I had some issues with it at first as well. Had the velocity values in a function that kept being called by updates, tried to smooth movement rather than input etc. This could do with some cleaning up but it works.
You are a golden god. That was a lot of help.
I had the same problem and decided to smooth the rotation itself. It’s pretty simple, just change the part where the rotation happens to:
Quaternion newRotation = Quaternion.Euler(pitch, yaw, roll);
Quaternion clampedTargetRotation = Quaternion.RotateTowards(transform.localRotation, newRotation, maxRotationSpeed * Time.deltaTime);
transform.localRotation = clampedTargetRotation;
I used a maxRotationSpeed of 150.0f, so the buttons would feel about the same as fully pressing a joystick.
The downside is that this doesn’t smooth the acceleration, it only fixes the rotation itself.