- Home /
How to tilt/Rotate an object based on touch input
Hello everyone, I am a unity noob and have been stuck for a while trying to implement this. I have a car which I swerve around while on the screen left to right. I want to rotate/tilt the car a bit while moving in left or right, by changing the transform rotation Z-axis. I know I can animate but I am learning and thought about doing it in code and I also found that doing things like this in code is better for performance than making Animations.
Character movement scripts:-
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
_lastFrameFingerPositionX = Input.mousePosition.x;
}
else if (Input.GetMouseButton(0))
{
_moveFactorX = Input.mousePosition.x - _lastFrameFingerPositionX;
_lastFrameFingerPositionX = Input.mousePosition.x;
}
else if (Input.GetMouseButtonUp(0))
{
_moveFactorX = 0f;
}
}
private void Awake()
{
swerveSetup = gameObject.GetComponent<SwerveSetup>();
rb = gameObject.GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
float swerveAmount = Time.deltaTime * swerveSpeed * swerveSetup.MoveFactorX;
swerveAmount = Mathf.Clamp(swerveAmount, -maxSwerveAmount, maxSwerveAmount);
transform.Translate(-swerveAmount, 0, 0);
}
Comment