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 anonimogk · May 29, 2021 at 07:04 PM · physicstransform.positiontransform.rotationpush

moving object while it has another movement

I want to code hypercasual mobile game "push'em all". I have 2 questions. However firstly, I couldnt figure out how pusher object moves. In game, if I release my finger, player stops and pusher object pushes its enemy. But the thing is ; while it is moving forward and backward, its both position and rotation also is changing according to player object. I mean after you release your finger, while pusher object is moving, you can change the rotation and position of player again, and somehow the pusher object moves accordingly. How can it manage 2 different rotation and position I am totally lost. In my code, it pushes forward and back when I release my finger, but the moment when I press again, the pusher object comes back to its original position immediately beacuse of the first2 line of if statement(Input.GetMouseButton(0)).

Secondly, my pusher object doesnt move forward every time when I release my finger. Someone said "use Input.GetMouseButtonUp(0)" so that it works only mouse release, but in this case, pusher object doesnt come back.

If you could give any suggestion, I really be appreciated. Thanks in advance:)

it is what I did so far;

 private void Update()
         {    **rb is rigidbody
             if (Input.GetMouseButtonDown(0))
             {
                 firstTouchPos = Input.mousePosition;
             }
 
             if (Input.GetMouseButton(0))
             {
                 TransformMovingPusher.position = StablePusher.position;
                 TransformMovingPusher.rotation = StablePusher.rotation;
 
 
                 _pushing = false;
                 deltaTouchPos = Input.mousePosition - firstTouchPos;
                 direction = new Vector3(deltaTouchPos.x, 0f, deltaTouchPos.y);
 
                 rb.velocity = direction.normalized * Speed;
                 if (rb.velocity != Vector3.zero)
                 {
                     rb.rotation = Quaternion.LookRotation(rb.velocity);
                 }
             }
 
             //mouse released
             else
             {
                 _pushed = true;
                 rb.velocity = Vector3.zero;             
                 StartCoroutine("Push", 2);
             }
         }
         
         private IEnumerator Push(float duration)
         {
             if (!_pushing) 
             {
                 Debug.Log("forward!");
                 MovingPusher.AddForce(StablePusher.forward * 500);
                 MovingPusher.velocity = transform.TransformDirection(0, 0, 3);
                 yield return (new WaitForSeconds(2));
                 _returning = true;
                 _pushing = true;
                     
             }
                         
             else if (_returning)            
             {
                 
                 Debug.Log("backward!");
                 TransformMovingPusher.position = Vector3.MoveTowards(TransformMovingPusher.position, StablePusher.position, 0.05f);
 
                 yield return (new WaitForSeconds(1.5f));                                  
                 _returning = false;
             }
 
             else
             {
                 TransformMovingPusher.position = StablePusher.position;
                 yield break;
             }
             
 
         }
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 SadeqSoli · May 31, 2021 at 03:25 PM

Hi @gkanonimo , I think you should check your booleans as I saw them you're giving them wrong values in the wrong places. and if you don't want any rotation use Quaternion.Identity and make sure using Fixed update since you are using rigidbody to avoid any glich or errors like that, BTW you can also use Input.GetMouseButtonDown(0) and Input.GetMouseButtonDown(1) to make sure there is no confusion for user input system. hope it hepls you, good luck, cheers.

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

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

Vertical Camera Orbit 0 Answers

Push/Pull Physics - Cube 1 Answer

gameobject still rotating over z axis 0 Answers

How to get updated Transform of a spawned object? 1 Answer

RigidBody.velocity or transform.position 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