- Home /
Question by
hacked · Jun 23, 2015 at 12:19 PM ·
c#unityengine.object
Accelerometer sensor in unity 3d?
Hope you all are doing good i am working on a game which needs both Input.mouse Position and tilting and i tried to use it with two different script but unfortunately i am having as issue my character vibrates on the z direction. i am writing both the codes below.
public class conteoler : MonoBehaviour {
public Camera cam;
private float maxWidth;
private bool camControl;
// Use this for initialization
void Start () {
if (cam == null){
cam = Camera.main;
}
camControl = false;
Vector3 upperCorner = new Vector3(Screen.width, Screen.height, 0.0f);
Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
float hatWidth = GetComponent<Renderer>().bounds.extents.x;
maxWidth = targetWidth.x - hatWidth;
}
// Update is called once per frame
void FixedUpdate () {
if(camControl){
Vector3 rawPosition = cam.ScreenToWorldPoint (Input.mousePosition);
Vector3 targetPosition = new Vector3 (rawPosition.x, 0.0f, 0.0f);
float targetWidth = Mathf.Clamp (targetPosition.x, -maxWidth, maxWidth);
targetPosition = new Vector3(targetWidth, targetPosition.y, targetPosition.z);
GetComponent<Rigidbody2D>().MovePosition (targetPosition);
}
}
public void ToggleControl (bool toggle) {
camControl =toggle;
}
Second code.which i also put on the player.
public class accelerometer : MonoBehaviour {
private float speed = 10.0F;
void Update() {
Vector3 dir = Vector3.zero;
dir.x = -Input.acceleration.y;
dir.x = Input.acceleration.z;
if (dir.sqrMagnitude > 50)
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate(dir * speed);
}
Comment
@Graham Dunnett this was the original post apologies for that.
Can you describe what you're trying to accomplish here?
I am trying to accomplish both left, right movement on tilting and on input mouse button @$$anonymous$$ike Geig ♦♦
You could zero the z element at the end of each Update/FixedUpdate.
Also make sure that the vibration is not actually on the camera. This is sometimes the case.