- Home /
tilt function for moving the character left and right
hi developers,
I want help regarding the tilt functionality. Please help me as I am stuck in this from last few days. I am trying to move my character left and right (similar to temple run 2). The thing is that I am moving my character totally on spline except in y-axis. I am using the concept of iTween spline. Now I am stuck in translating my character on x-axis. As I am using the concept of iTween spline (putonpath()) i need to move the waypoints towards right and left based on tilt. I am working on the curved path thats why i need the concept of spline. For moving my waypoints left and right I am using the following code.
using UnityEngine; using System.Collections; using System;
public class WayPoint : MonoBehaviour {
bool gyroBool;
GameObject distanceChecker;
Gyroscope gyro;
int nameNo=0, transValue = 30, objNameLength, lengthDiff;
string objName, defName = "waypoint", appendName = null;
void Awake() {
parentScript = transform.parent.GetComponent();
rootScript = transform.root.GetComponent();
objName = name;
objNameLength = objName.Length;
if(objNameLength>defName.Length) {
lengthDiff = objNameLength-defName.Length;
for(int i = defName.Length; i=0 && xPos<=30) {
}
else
if(xPos>30 && xPos<=180) {
xPos = 30;
}
else
if(xPos>=330) {
xPos = xPos - 360;
}
else
if(xPos<330 && xPos>180) {
xPos = 330;
}
else
return;
float xValue = fraction * xPos;
requiredPosition = distanceChecker.transform.TransformPoint(Vector3.right * xValue);
transform.position = requiredPosition;
}
}
much of my code is getting deleted automatically in between.... sorry..... some problem is going on here for posting the code
Again I am posting my code
using UnityEngine;
using System.Collections;
using System;
public class WayPoint : $$anonymous$$onoBehaviour {
bool gyroBool;
GameObject distanceChecker;
Gyroscope gyro;
int nameNo=0, transValue = 30, objNameLength, lengthDiff;
string objName, defName = "waypoint", appendName = null;
void Awake() {
objName = name;
objNameLength = objName.Length;
if(objNameLength>defName.Length) {
lengthDiff = objNameLength-defName.Length;
for(int i = defName.Length; i<objNameLength;i++) {
appendName = appendName+""+objName[i];
}
}
distanceChecker = new GameObject();
distanceChecker.name = "distanceChecker"+appendName;
distanceChecker.transform.parent = transform.parent;
distanceChecker.transform.position = transform.position;
distanceChecker.transform.rotation = transform.rotation;
gyroBool = SystemInfo.supportsGyroscope;
if(gyroBool) {
gyro = Input.gyro;
gyro.enabled = true;
}
}
void Update() {
if(gyroBool) {
GyroWork();
}
}
Vector3 requiredPosition;
int xPos;
float fraction = 0.1f;
void GyroWork() {
xPos = (int) gyro.attitude.eulerAngles.y;
if(xPos>=0 && xPos<=30) {
}
else
if(xPos>30 && xPos<=180) {
xPos = 30;
}
else
if(xPos>=330) {
xPos = xPos - 360;
}
else
if(xPos<330 && xPos>180) {
xPos = -30;
}
else
return;
float xValue = fraction * xPos;
requiredPosition = distanceChecker.transform.TransformPoint(Vector3.right * xValue);
transform.position = requiredPosition;
}
}
Answer by cartonino · Nov 07, 2016 at 07:40 AM
@tenthplanet0 I have some problem... You resolved it?
Your answer
![](https://koobas.hobune.stream/wayback/20220613104332im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
deviceUniqueIdentifier 1 Answer
What device is being used? 1 Answer
Rendering with the CPU in Unity 0 Answers
How to make an actor spawn at a fixed position 1 Answer
How to detect if the device is android mobile or tablet 2 Answers