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 Saileeich · Jan 07, 2021 at 07:30 PM · scripting problemrigidbody2dbeginnerrigidbody-collision

I need help with rigidbody and collision or something idk

The way I have my player set up is an empty game object as the main gameobject and it has a movement script using transform.Translate, a sprite for the player as a child of that gameobject, a centerpoint as a child to the gameobject with a bow sprite and fire point attached to it to shoot. And to that center point i have a script that makes it rotate towards the mouse (script from brackeys tutorial). but the problem is that when the player collides with something, the sprite and the empty gameobject stays, but the center and its children keep going because the center has a regidbody attached to it to do the rotation of the center point. So im wondering if there is a replacement option for the rigidbody.rotate so that they stay when the player collides with walls and enemies e.t.c. Here is the rotation script:

 public class ArcherAttack : MonoBehaviour
 {
     public Camera cam;
     public Rigidbody2D rb;
 
     Vector2 mousePos;
 
     void Update()
     {
         MousePosition();
     }
 
     void FixedUpdate()
     {
         FireRotation();
     }
 
     void MousePosition()
     {
         mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
     }
 
     void FireRotation()
     {
         Vector2 lookDir = mousePos - rb.position;
         float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
         rb.rotation = angle;
     }
 }
 

Here is the tutorial I followed

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
1
Best Answer

Answer by bbudian0 · Jan 07, 2021 at 08:35 PM

It’s most likely that you’re using transform.Translate(). If you have a rigidbody attached and you’re trying to utilize collision I’d recommend using the rigidbody.AddForce() or set the rigidbody.velocity in order to move your sprite correctly.

transform.Translate() doesn’t take rigidbody collision into account.

Comment
Add comment · Show 3 · 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 Saileeich · Jan 07, 2021 at 08:49 PM 0
Share

Thank you so much! Will Try that out tomorrow.

avatar image bbudian0 Saileeich · Jan 07, 2021 at 09:01 PM 0
Share

Not a problem! Let me know if you have any issues.

avatar image bbudian0 Saileeich · Jan 07, 2021 at 09:48 PM 0
Share

Not sure I follow the rotation bit but if your character is moving on its own after collision mess around with setting the constraints of the rigidbody. I typically check all rotational axis and usually the y position.

But that is dependent on your use case.

avatar image
1

Answer by Derek_Brouwer · Jan 07, 2021 at 08:30 PM

There are 2 options I can see here. First thing to know is that you don't need a rigidbody to handle the rotation of a object. Rigidbodies are unity's physics system, and all it's doing is modifying the rotation on the transform which already exists.


So what I would do is simply set the rotation on the transform of your center point: transform.eulerangles = new Vector3 (0, 0, angle); //eulerangles are a nicer way of working with rotations using a Vector3 instead of a Quaternion.


Doing that you should be able to remove the rigidbody component (and whatever collider you likely have on the bow).


If you wanna keep the rigidbody for whatever reason, simply setting it to isKinematic to true (in the inspector) should disable collisions and physics on it, but allow the rotation and other physics systems to still me modified by code.

That would be the simplest solution but seems messy to me, if you have no need for the physics.

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 Saileeich · Jan 07, 2021 at 09:39 PM 0
Share

I tried setting the rigidbody to kinematic, but then i couldn’t rotate the bow whilst i was moving the player. But i will definitly give your first suggestion a try! 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

221 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

I'm following a tut - converting to 3d - and confuzzled by RigidBody2d - vector 3 0 Answers

Is what a parent gameobject can consult or modify components attributes of it desactivated gameobject children 0 Answers

How do I instantiate/fire when Joystick input ends? 0 Answers

How to use a timer with a button 2 Answers

True and false work weird? 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