• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by laurienvictoria · Jun 02, 2016 at 10:17 PM · transformspeedaccelerometer

Object controlled by accelerometer: speed slows when further from target point, how do I increase the speed when phone is tipped in opposite direction?

Hi!

I've written a C# script that controls an object's movement based on the accelerometer values (for an endless runner game). I've calculated the percentage the object is away from the centre point, and currently use this line:

 speedX = speedX - (SpeedSlow()/100 * speedX) +0.3f;

to set the speed of the object to be faster when close to a set centre point, and slower as it moves away from this transform position.

The problem I'm having is that I want the speed to slow down as the object moves towards the edge (which it does): but when you tip the phone the opposite direction to bring the object back towards the centre point, I want the speed to be set immediately faster (and this is the part I can't work out how to achieve).

I tried stating:

 if (speedX < 0.2f)
     {
     transform.position = Vector3.MoveTowards(transform.position, targetBagPos, Time.deltaTime * 3.5f);
 }

But this doesn't work: it causes the movement to behave erratically, and moves the object in the opposite direction on the x axis than expected (right when the phone is tilted left etc)

This is the script with the problematic line commented out:

 using UnityEngine;
 using System.Collections;
 
 public class Accelerometer : MonoBehaviour {
     
     public Vector3 targetBagPos;
 
     void Update () 
     {
         float tempx = Input.acceleration.x;
         float tempz = Input.acceleration.z + 0.7f;
         float speedX = 6.0f;
 
         speedX = speedX - (SpeedSlow()/100 * speedX) +0.3f;
 
         //        if (speedX < 0.2f)
         //        {
         //            transform.position = Vector3.MoveTowards(transform.position, targetBagPos, Time.deltaTime * 3.5f);
         //        }
 
         transform.Translate(tempx * Time.deltaTime * speedX,0,0);
         Vector3 posX = transform.localPosition;
         posX.x = Mathf.Clamp (posX.x, -1.5f, 1.5f);
         transform.localPosition = posX;
         
         transform.Translate(0,0,-tempz  * Time.deltaTime * speedX);
         Vector3 posZ = transform.localPosition;
         posZ.z = Mathf.Clamp (posZ.z, -1.5f, 1.5f);
         transform.localPosition = posZ;
 
         SpeedSlow();
     }
 
     public float SpeedSlow()
     {
         float maxDistance = 1.5f;
         Vector3 currentPos = this.transform.localPosition;
         Vector3 centrePos = new Vector3(0,0,0);
 
         Vector3 distanceBetween = centrePos - currentPos;
         float length = distanceBetween.magnitude;
 
         float perCent = length / maxDistance * 100;
 
         Debug.Log (perCent);
 
         return perCent;
     }


Does anyone have any ideas on ways I could script the movement so that when the object is tilted back towards the centre point: the speed is immediately given an increase.

I hope I explained that clearly enough!

All the best, Laurien

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

48 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotating an object 1 Answer

Acceleration of Transform in FixedUpdate (SOLVED) 0 Answers

Jerky motion using moveTowards at high speed 0 Answers

Set a relative (custom) speed to a rotation 1 Answer

transform.eulerAngles rotating speed (quick question) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges