Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Wibber · Jun 16, 2013 at 07:42 PM · c#androidrigidbodyaddforceaccelerometer

Rigidbody only moving in one direction

So I'm not a coder so this is hell for me, but I am trying to get my head around this so bear with me please.

I got this code

 using UnityEngine;
 using System.Collections;
 
 public class PlayerScript : MonoBehaviour {
     
     public static float distanceTraveled;
     private Touch curTouch;
     public float speed;
     public float maxSpeed;
     public float maxSpeedConstant;
     
     //Virtual buttons left and right half of the screen
     private Rect leftHalf = new Rect(0F,0F,Screen.width/2,Screen.height);
     private Rect rightHalf = new Rect(Screen.width/2,0F,Screen.width/2,Screen.height);
     
     public void Update() {        
         distanceTraveled = transform.localPosition.x;
         }
     public void FixedUpdate() {
         Movement ();
     }
 
     public void Movement(){
         
         if(rigidbody.velocity.magnitude > maxSpeed)
         {
             rigidbody.velocity = rigidbody.velocity.normalized * maxSpeed;
         }
         
         //Accelerometer Control up/down
         Vector3 dirAcc = Vector3.zero;
         
         dirAcc.y = Input.acceleration.y*10;
         
         rigidbody.AddForce(0.0F,0.0F,dirAcc.y);
         
         if(Input.touchCount != 0){
             if (leftHalf.Contains (curTouch.position)){
                 maxSpeed = maxSpeed*2;
                 rigidbody.AddForce(-speed,0,0);
             }else{
                 if (rightHalf.Contains(curTouch.position)){
                     maxSpeed = maxSpeed*2;
                     rigidbody.AddForce(+speed,0,0);
                 }
             }
         }else{
             if(Input.touchCount == 0){
                 maxSpeed = maxSpeedConstant;
                 rigidbody.AddForce(speed,0,0);
             }            
         }        
     }
 }


What I'm trying to achieve is that the play is able to control the up/down movement of the character via accelerometer and right/left movement by touching the right/left side of the screen.

With the above code the touch area does not matter the character will always move backwards and accelerometer input is entirely ignored. What bothers me is that the above code (accelerometer part only) worked with transform.Translate instead of rigidbody.AddForce. But from what I've read on the internet I'm going to need rigidbodies if I want collisions.

So any help or advice regarding code structure/syntax or solution towards my problem is appreciated.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ExpiredIndexCard · Jun 16, 2013 at 08:19 PM

If you want collisions, you do need a rigidbody but you do not have to make the movement based on physics. Use Translate only and add a rigidbody to the gameobject. Simple. That means that you can easily make your character move, and you will still have collisions.

Comment
Add comment · Show 2 · Share
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
avatar image Wibber · Jun 16, 2013 at 10:25 PM 0
Share

Thanks I did not know this! Still, the problem of the player moving in only one direction whilst pressing the screen persist.

Are my Rectangles set up the right way?

 private Rect leftHalf = new Rect(0F,0F,Screen.width/2,Screen.height);
 private Rect rightHalf = new Rect(Screen.width/2,0F,Screen.width/2,Screen.height);

And is my Input check correct?

         if(Input.touchCount != 0){
             if (leftHalf.Contains (curTouch.position)){
                 transform.Translate(-Vector3.right*speed*Time.deltaTime);
             }else{
                 if (rightHalf.Contains(curTouch.position)){
                     transform.Translate(Vector3.right*speed*Time.deltaTime);
                 }
             }
         }else{
             if(Input.touchCount == 0){
                 transform.Translate(Vector3.right*speed*Time.deltaTime);
             }            
         }
avatar image ExpiredIndexCard · Jun 17, 2013 at 06:14 AM 0
Share

I have never worked with mobile. I'm sorry but I guess someone else has to answer thatfor you. Sorry

avatar image
0

Answer by Wibber · Jun 18, 2013 at 12:16 PM

I feex. Instead of using Touch curTouch; to define the touch position I had to use a Vector2,use Input.GetTouch(0) for the if statement and now I set curTouch everytime a touch occurs.

Here is what the essential part looks like now :

 public void Movement(){
         
         //Accelerometer Control up/down
         Vector3 dirAcc = Vector3.zero;
         
         dirAcc.y = Input.acceleration.y*.5F;
         
         transform.Translate(0F,0F,dirAcc.y);
         
         if(Input.touchCount != 0){
             Vector2 curTouch = Input.GetTouch(0).position;
             if (leftHalf.Contains (curTouch)){
                 //transform.Translate(-Vector3.right*speed*Time.deltaTime);
                 transform.Translate (-moveVec*2*Time.deltaTime);
             }else{
                 if (rightHalf.Contains(curTouch)){
                     //transform.Translate(Vector3.right*speed*Time.deltaTime);
                     transform.Translate (moveVec*2*Time.deltaTime);
                 }
             }
         }else{
             if(Input.touchCount == 0){
                 //transform.Translate(Vector3.right*speed*Time.deltaTime);
                 transform.Translate (moveVec*Time.deltaTime);
             }            
         }        
     }
Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

How to move forward constantly and right left when it's necessary 0 Answers

How prevent addforce when clicking resume button? 1 Answer

Projectile not moving properly in top down 2d shooter 2 Answers

Distribute terrain in zones 3 Answers

Combine iTween with physics acceleration 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges