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.