Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ipk4fun_ · Sep 20, 2019 at 02:02 PM · 3dgametouch controlsgamedesign

How do I add ForceMode.VelocityChange to this code

How do I add ForceMode.VelocityChange to this code, I made a code that moves the player right or left depending on the side of screen u touch on the phone, but when I touch the player continues going and not stoping so here is the code I want ForceMode.VelocityChange to add to @Brackeys

public class Touchcon : MonoBehaviour {

 public float touchspeed= 300f;
 public GameObject player;

 private Rigidbody Rb;

 private float ScreenWidth;



 // Start is called before the first frame update 
 void Start()
 {
     ScreenWidth = Screen.width;
     Rb = player.GetComponent<Rigidbody>();
 }

 // Update is called once per frame
 void Update()
 {
     int i = 0;
     while (i < Input.touchCount)
     {

         if (Input.GetTouch(i).position.x > ScreenWidth / 2)
         {

             RunPlayer(1.0f);
         }
         if (Input.GetTouch(i).position.x < ScreenWidth / 2)
         {
             RunPlayer(-1.0f);

         }

         ++i;


     }
 }
 private void FixedUpdate()
 {
     #if UNITY_EDITOR
     RunPlayer(Input.GetAxis("Horizontal"));
     #endif
 }
 private void RunPlayer(float honrizontalInput)
 {
     Rb.AddForce(new Vector3(honrizontalInput * touchspeed * Time.deltaTime, 0 ));





 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by I_Am_Err00r · Sep 20, 2019 at 03:49 PM

I don't have an IDE to test this out with, but I think I see what is going on, let me know if this helps:

  void Update()
  {
      if(Input.touchCount > 0
      {
         Touch touch = Input.GetTouch(0)
          if (Input.GetTouch(0).position.x > ScreenWidth / 2)
          {
              RunPlayer(1.0f);
          }
          if (Input.GetTouch(0).position.x < ScreenWidth / 2)
          {
              RunPlayer(-1.0f);
          }
      }
  }

I don't know if you have other commands that would be called if you had multiple fingers on the screen, but I believe this will run the code you are trying to only if you are actually touching the screen, regardless of how many fingers that is.


If it doesn't work, let me know, I can take another stab at it in about an hour when I have access to Unity and an IDE.

Comment
Add comment · Show 1 · 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 ipk4fun_ · Sep 20, 2019 at 08:06 PM 0
Share

@I_Am_Err00r So if I add this to my code, then my player will cease to have momentum when they move left or right? $$anonymous$$eaning, they wont keep going right or left while Im trying go to the opposite direction?

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

172 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 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 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 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

Can you play a cutscene when entering an area? 0 Answers

3D game is 2D 1 Answer

How to drag and move a character left and right using touch controls in an endless runner game? 0 Answers

(in the end of the code...) when i try to run the program that tells me that the object is destoryed and i cant access it someone help? 1 Answer

How to do a SLiding door animation in Unity 3d with scripting. 1 Answer


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