- Home /
Smoothly rotate object based on GetAxis
Hello. I was wondering how I could rotate an object (rigidbodied) smoothly based on joystick axis.
using UnityEngine;
using System.Collections;
public class InAirTest : MonoBehaviour {
//raycasting variables
public bool offGround = false;
//rotation variables
public float speed = 100;
// public float tiltAngle = 30f;
// public float smooth = 100f;
// float tiltX = Input.GetAxis("Horizontal");
// float tiltY = Input.GetAxis("Vertical");
// public GameObject bike;
// Update is called once per frame
void Update () {
RaycastHit hit;
Vector3 down = transform.TransformDirection(Vector3.down) * 2f;
Debug.DrawRay(transform.position, down, Color.red);
/* if (Physics.Raycast(transform.position,(down), out hit))
{
distance = hit.distance;
Debug.Log("Distance: " + distance + hit.collider.gameObject.name);
}*/
//testing if off ground
//Quaternion target = Quaternion.Euler(tiltX, 0, tiltY);
if(Physics.Raycast(transform.position, down, out hit))
{
if(hit.collider.tag != "ground")
{
offGround = true;
}else if(hit.collider.tag == "ground")
{
offGround = false;
Debug.Log("...on ground......");
}
}
//tell console if off ground
if(offGround == true)
{
if(Input.GetAxis("Horizontal") > 0)
{
transform.Rotate(0.0f, Input.GetAxis("Horizontal") * speed, 0.0f);
}
if(Input.GetAxis("Horizontal") < 0)
{
}
Debug.Log("in air :D");
}
}
}
This is the code I have so far. It rotates the way I want it to but it is delayed by 1.5 seconds roughly because of the conditional statements I believe also it seems to jump to the next angle. I also dont like how it doesnt gradually progress in velocity of the turn. Any hekp would be appreciated. Thanks.
Firstly I'd multiply it by Time.deltaTime to make it framerate independent. Secondly you may find using GetAxis to modify a velocity, and then using the velocity to deter$$anonymous$$e the rotation amount may improve your situation, rather than GetAxis directly controlling rotation.
By a velocity, do you mean the velocity the object is moving?
Answer by hodge47 · Jan 14, 2016 at 08:33 AM
For those that come across this in the future, I have found a solution using transform.rotate and a vector3. I used the -Input times the bike speed times delta time for smooth rotation.
transform.Rotate(Vector3.right, -Input.GetAxis("RiderLeanfb") * this.bikeSpeed * Time.deltaTime, Space.Self);