Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 dBlue · Jul 15, 2015 at 07:28 PM · c#rigidbodyrigidbody2dbeginner

Objects stop, but then pass through eachother, Rigidbody2D

I'll give the code for both of my objects, but essentially what happens is one object will collide with the other, and then they'll pass through each other. I think the problem is that I'm coding their movement incorrectly, but it could be one of the rigidbody settings. I think it should be an easy fix, but I couldn't find the solution online anywhere... Here's the inspector settings on the "ball" object (a square): alt text Here's the inspector settings on the "Obstacle" object (a big rectangle): alt text

Here's my code for the ball and the obstacle:


Ball


using UnityEngine; using System.Collections;

public class Falling : MonoBehaviour {

 Vector3 velocity = Vector3.zero;
 public Vector3 gravity;
 public Rigidbody2D rb;
 
 // Use this for initialization
 void Start () {
         rb = GetComponent<Rigidbody2D>();
 }

 // Update is called once per frame, PHYSICS here
 void FixedUpdate () {
         velocity += gravity * Time.deltaTime;
         rb.AddForce(transform.position += velocity * Time.deltaTime);

 }

}


Obstacle


using UnityEngine; using System.Collections;

public class Upward : MonoBehaviour {

 Vector3 velocity = Vector3.zero;
 public Vector3 AntiGravity;
 public Rigidbody2D rb;

 // Use this for initialization
 void Start () {
     rb = GetComponent<Rigidbody2D>();
     float RandomFl = Random.Range(-0.755f, 0.755f);
     transform.position = new Vector3 (RandomFl, -3.4f, -1f);
 }
 
 // Update is called once per frame
 void Update () {
     if (transform.position.y <= 3.35) {
         velocity -= AntiGravity * Time.deltaTime;
         rb.AddForce(transform.position -= velocity * Time.deltaTime);
     }
 }

}

If you guys could help me, that would be awesome! I can't find the solution online.

b.png (58.2 kB)
o.png (55.0 kB)
Comment
Add comment · Show 4
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 barbe63 · Jul 16, 2015 at 04:15 PM 1
Share

Wow, I see lots of weird stuff here...

First why increment your velocity every FixedUpdate()... and why multiply it by Time.deltaTime knowing you are in fixedUpdate? Then why in your AddForce you pass an assignment? (-= or += are assignments)

avatar image hexagonius · Jul 16, 2015 at 04:52 PM 1
Share

I'd also say the AddForce is the problem in both cases. Just use the velocity as a force, one positive, one negative, NOT the position too. Assume your game is not running around the world's zero but rather somewhere around the hundreds. Do you want to use THAT position as the new force to move by?

avatar image dBlue · Jul 16, 2015 at 06:28 PM 0
Share

Alright guys, I see what you mean. I should just be using the built in physics, I'm not sure what I was thinking with making my own. Thanks, I guess this can be considered answered.

avatar image barbe63 · Jul 16, 2015 at 06:56 PM 1
Share

For the record this is how you use gravity when you need your own:

 Vector3 gravity = new Vector3(0,-20,0);
 
 void FixedUpdate()
 {   
     rb.AddForce(gravity, Force$$anonymous$$ode.Acceleration);
 }
 

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

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

How to not allow ball to be thrown after first Toss 2 Answers

My sphere loses velocity when jumping 2 Answers

Object shattering under a certain weight/force 3 Answers

Multiple Cars not working 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