- Home /
Question by
xfixtrion4 · Aug 10, 2019 at 07:07 AM ·
vrcontrollerbike
Smooth turn (bike controller + yocto)
Hi everyone !
I have an issue in my vr simulator. I use a yoctoPuce to get impulse from my bike (real). When i go forward it works pretty well but the rotation is jerky. If anyone have a solution this will be wondeful ! This is my Script:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BikeControler : MonoBehaviour
{
public bool isReplay;
public int minAngle;
public int maxAngle;
public float magnitude;
public bool alreadyTurn;
public float coef;
private Data data;
#region bike component
public GameObject bikeFrame;
public GameObject frontWheel;
public GameObject backWheel;
public GameObject player;
public GameObject bike;
public float speed;
public float turnSpeed;
public GameObject wheel;
public GameObject point;
private float coefRotation = 1.2f / Mathf.Cos(21.257f);
public Rigidbody rigid;
public ReplayManager replayManager;
public GameObject cadretest;
#endregion
#region Yocto component
string errmsg = "";
string target;
YAnButton input1;
YAnButton input2;
YAnButton input3;
YAnButton input4;
YAnButton input5;
private long nbOfPulse;
private long lastNbOfPulse;
private int sens;
private float speedy;
#endregion
private void Start()
{
data = GameObject.FindGameObjectWithTag("Data").GetComponent<Data>();
//initialize my yoctoPuce
if (YAPI.RegisterHub("usb", ref errmsg) != YAPI.SUCCESS)
{
Debug.Log("RegisterHub error: " + errmsg);
Environment.Exit(0);
}
input1 = YAnButton.FirstAnButton();
if (input1 == null)
{
Debug.Log("No module connected (check USB cable) ");
Environment.Exit(0);
}
target = input1.get_module().get_serialNumber();
input1 = YAnButton.FindAnButton(target + ".anButton1");
input2 = YAnButton.FindAnButton(target + ".anButton2");
input3 = YAnButton.FindAnButton(target + ".anButton3");
input4 = YAnButton.FindAnButton(target + ".anButton4");
input5 = YAnButton.FindAnButton(target + ".anButton5");
nbOfPulse = input5.get_pulseCounter();
lastNbOfPulse = input5.get_pulseCounter();
}
void Update()
{
if (!isReplay)
{
//Get pulse of the yocto from the bike
nbOfPulse = input5.get_pulseCounter();
//Difference between the last pulse counter and now
long pulseDif = nbOfPulse - lastNbOfPulse;
lastNbOfPulse = nbOfPulse;
// Rotation value from the yoctopuce of the bike
float frameRotation = input2.get_calibratedValue();
float angle;
// Slowdown if the player stop pedaling
if (pulseDif == 0)
{
//Speed change with weather
if (speedy > 0)
if (data.weatherData == 2)
speedy = speedy - 0.05f;
else if (data.weatherData == 3)
speedy = speedy - 0.01f;
else
speedy = speedy - 0.2f;
}
else // Speed from impulse
{
speedy = pulseDif;
}
// The rotation of the bike frame
bikeFrame.transform.localEulerAngles = new Vector3(-21.257f, 0, (500 - frameRotation) / 10);
//Here is my issue-> rotation
if (frameRotation < minAngle || frameRotation > maxAngle)
{
if (frameRotation > 500)
{
angle = (360 - bikeFrame.transform.localEulerAngles.z);
sens = -1;
}
else
{
angle = -bikeFrame.transform.localEulerAngles.z;
sens = 1;
}
//here
angle = Mathf.Deg2Rad * angle;
float centerCircle = coefRotation * (1 / angle);
point.transform.localPosition = new Vector3(centerCircle, 0, 0);
bike.transform.RotateAround(point.transform.position, Vector3.up, sens * (speedy) * turnSpeed);
alreadyTurn = true;
}// Forward move
else
{
if (rigid.velocity.magnitude < magnitude)
{
if (alreadyTurn)
{
rigid.AddForce(transform.forward * (speedy) * turnSpeed * coef);
alreadyTurn = false;
}
else
rigid.AddForce(speed * (speedy) * 10 * cadretest.transform.forward, ForceMode.Acceleration);
}
}
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612210352im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
SteamVR 2.3 - Vive controllers aren't tracking after scene load 1 Answer
Gear VR controller buttons not working in Unity 2018.2 LW VR template 0 Answers
OVR Controller ghost flyer 0 Answers
how to save the values of hmd and controller position rotation in a list 0 Answers
How to map input for new Samsung Gear VR controller? 3 Answers