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 /
  • Help Room /
avatar image
-1
Question by alialikarami76 · Nov 18, 2015 at 01:07 PM · transformpositioncontrollercontrols

how to make the player move in 2 directions only?

I want to make the player move in these directions: (0,0,50) & (0,0,-50) so that when the character is moving forward in one of these directions with every players tap on the screen the character switches between these directions and i don't know how to write these scripts , can anyone please help?

i've got this so far but it doesn't work.

 public class bikeController : MonoBehaviour
 {
     public float speed = 30f;
 
     bool bikePosition = true;
 
     // Use this for initialization
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
 
         bool bikeChangingPosition = Input.GetButton("Fire1");
 
         if (bikeChangingPosition && bikePosition == true)
         {
             transform.rotation = Quaternion.Euler(0, 0, 290);
 
             rigidbody.velocity = new Vector3(6, 3, 290);
 
             bikePosition = false;
         }
 
         if(bikeChangingPosition && bikePosition == false)
         {
             transform.rotation = Quaternion.Euler(0, 0, 70);
 
             rigidbody.velocity = new Vector3(3, 6, 70);
 
             bikePosition = true;
         }
     }

Comment
Add comment · Show 3
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 ijidau · Nov 18, 2015 at 01:11 PM -1
Share

This is quite easy to achieve, but this site is not a homework service. You need to start learning the basics yourself. I highly recommend you watch as many of the tutorials as possible. http://unity3d.com/learn/tutorials

avatar image alialikarami76 ijidau · Nov 18, 2015 at 03:31 PM 0
Share

look at this

avatar image Owen-Reynolds · Nov 18, 2015 at 02:59 PM 0
Share

$$anonymous$$oved this to the HelpRoom area, which is a fine place for it.

The main site is for things you can't look up or learn from the Docs. For new users, with no game dev experience, it's difficult to know the difference.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Jessespike · Nov 18, 2015 at 04:50 PM

The problem is Input.GetButton("Fire1") only sends true when the buttons is held. You want it to toggle, so you'll need to move bool bikeChangingPosition out of the function and toggle it when Input.GetButtonDown("Fire1") is pressed. Then you can adjust the velocity depending which way it's leaning.

 public class bikeController : MonoBehaviour
 {
     public float speed = 1f;
     bool bikeChangingPosition;   // name isn't quite accurate
 
     // Update is called once per frame
     void Update()
     {
         GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 290f * speed);
 
         if (Input.GetButtonDown("Fire1"))
         {
             bikeChangingPosition = !bikeChangingPosition;  // toggle left/right lean
         }
         
         if (bikeChangingPosition)
         {
             // leaning right
             transform.rotation = Quaternion.Euler(0, 0, 290);
             GetComponent<Rigidbody>().velocity += new Vector3(50f * speed, 0f, 0f);
         }
         else
         {
             // leaning left
             transform.rotation = Quaternion.Euler(0, 0, 70);
             GetComponent<Rigidbody>().velocity += new Vector3(-50f * speed, 0f, 0f);
         }
     }
 }

I agree that reading the documentation is a must. But I still think, telling someone that this is easy to fix and to go watch videos does not answer the question. Since this was more of a code logic question than a Unity specific question, there's no documentation that would flat out tell you how to fix this. If this answers your question, then please mark it as answered.

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 alialikarami76 · Nov 18, 2015 at 05:52 PM 0
Share

thank you very much.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Move plane horizontally using 2D controller 0 Answers

How do you access the transform of a gameobject and put it in a variable? 3 Answers

how to move the enemy in the current position of the player but only ones? 0 Answers

Camera isn't move position? Why my camera isn't change position? 0 Answers

Resident Evil 4 Style Aiming Window - Rotational Problem When Aiming 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