- Home /
I have used this code to move my player in x direction, so how i can increase it's speed in x direction?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class monkeyscript : MonoBehaviour
{
private Rigidbody2D body;
[SerializeField]
private float movespeed = 20f;
private float drift_x;
void Awake()
{
body = GetComponent<Rigidbody2D>();
}
// code for moving player in x direction with the help of gyroscope.
void Update()
{
drift_x = Input.acceleration.x * movespeed;
body.velocity = new Vector2(drift_x, body.velocity.y);
}
}
And the portion where the i think the error is starts form line number 54 in my code. When I run my code I get zero console errors or any other type of errors. I have already tried to change the value of "movespeed" and it didn't worked. My main aim is that player should move in x direction when the device is tilted accordingly. But the speed of player in x direction is too slow. So please help me to correct my code or if my code is completely wrong please suggest me something better. I have also attached rigid body component picture.
[1]: /storage/temp/195872-unity-question.png
Answer by zjebali · May 03 at 06:37 PM
Try doing the multiplication here:
body.velocity = new Vector2(drift_x * moveSpeed, body.velocity.y);
Also double check in the inspector that your value is set correctly food moveSpeed.
Your answer
Follow this Question
Related Questions
Get absolute acceleration data from accelerometer/gyroscope but neglect rotational acceleration 1 Answer
How to judge intensity of shaking in android phones 1 Answer
How to get highest value for acceleration from Input.gyro 0 Answers
Is it possible to get the forward and backward movement of android device 1 Answer
Recalibrated 'zero' point for Input.acceleration messes up when goes to upside-down 0 Answers