HoverBike rotation return Y axis to 0
Hello everyone, i don't know how to resolve this problem :
I am making a HoverBike and took the basic hovering script with raycast, i tried to tilt the object when it turns but then the car would not rotate, so i tried to make another way to rotate but then i noticed that when i release one of the horizontal key, the object's Y Axis simply return to 0, slowly. This effect suit perfectly the tilting rotation, but also apply to turn rotation wich make my Object impossible to control. I am relatively new to unity so i may have made rookie mistake, and for that i apologize. Thanks you.
New HoverScript :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HoverMotor : MonoBehaviour
{
public float speed = 90f;
public float turnSpeed = 5f;
public float hoverForce = 65f;
public float hoverHeight = 3.5f;
private float powerInput;
private float turnInput;
private Rigidbody surreaRigidbody;
void Awake()
{
surreaRigidbody = GetComponent<Rigidbody>();
}
void Update()
{
powerInput = Input.GetAxis("Vertical");
turnInput = Input.GetAxis("Horizontal");
}
void FixedUpdate()
{
Ray ray = new Ray(transform.position, -transform.up);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, hoverHeight))
{
float proportionalHeight = (hoverHeight - hit.distance) / hoverHeight;
Vector3 appliedHoverForce = Vector3.up * proportionalHeight * hoverForce;
surreaRigidbody.AddForce(appliedHoverForce, ForceMode.Acceleration);
}
surreaRigidbody.AddRelativeForce(0f, 0f, powerInput * speed);
if (turnInput < 0)
transform.Rotate(-Vector3.up * turnSpeed * Time.deltaTime);
else if (turnInput > 0)
transform.Rotate(Vector3.up * turnSpeed * Time.deltaTime);
}
Old HoverScript :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HoverMotor : MonoBehaviour
{
public float speed = 90f;
public float turnSpeed = 5f;
public float hoverForce = 65f;
public float hoverHeight = 3.5f;
private float powerInput;
private float turnInput;
private Rigidbody surreaRigidbody;
void Awake()
{
surreaRigidbody = GetComponent<Rigidbody>();
}
void Update()
{
powerInput = Input.GetAxis("Vertical");
turnInput = Input.GetAxis("Horizontal");
}
void FixedUpdate()
{
Ray ray = new Ray(transform.position, -transform.up);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, hoverHeight))
{
float proportionalHeight = (hoverHeight - hit.distance) / hoverHeight;
Vector3 appliedHoverForce = Vector3.up * proportionalHeight * hoverForce;
surreaRigidbody.AddForce(appliedHoverForce, ForceMode.Acceleration);
}
surreaRigidbody.AddRelativeForce(0f, 0f, powerInput * speed);
surreaRigidbody.AddRelativeTorque(0f, turnInput * turnSpeed, 0f);
}
TiltScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TiltScript : MonoBehaviour {
private float tiltAngleVertical = 5f;
private float tiltAngleHorizontal = -25f;
public float tiltRes = 5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngleHorizontal;
float tiltAroundX = Input.GetAxis("Vertical") * tiltAngleVertical;
Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * tiltRes);
}
}
Your answer
Follow this Question
Related Questions
Cant add Torque and Rotation to a rigidbody in the same frame? 2 Answers
Rotating 3d person character 0 Answers
Making 2D Compass From Quaternion 1 Answer
Move an object around all 6 sides of a cube 2 Answers
Nonlinear movement from A to B 0 Answers