• 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 Kyieon · Jul 31, 2013 at 12:42 PM · physicsrigidbodyparentchildparenting

Child rigidbody clipping through walls.

I am trying to create a gravity gun like in Half Life 2. The gravityGun script parents a cube to the main camera. The main camera is parented to the player. The player is a character controller.

My problem is that the cube passes through objects if the player walks into the wall. This is my Player Controller script. Can someone help me with this problem by ether stopping the player from moving forward or rotating the cube if it is colliding with a wall or moving the cube around in your hands much like in Half life? I should mention that the cube is a Rigidbody.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(CharacterController))]
 
 public class firstPersonController : MonoBehaviour {
     
     public float movementSpeed = 10;
     public float mouseSensitivity = 2.5f;
     public float mouseVertRange = 60;
     public float jumpVelocity = 5;
     
     private float verticalRoatation;
     private float verticalVelocity;
     private float forwardSpeed;
     private float leftRightSpeed;
     private float rotateLeftRight;
     
     private Vector3 movement;
     
     private CharacterController characterController;
     
     // Use this for initialization
     void Start () {
         characterController = GetComponent<CharacterController>();
     }
     
     // Update is called once per frame
     void Update () {
         
         // Horizontal Rotation
         rotateLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
         transform.Rotate(0, rotateLeftRight, 0);
         
         // Vertical Rotation
         verticalRoatation += Input.GetAxis("Mouse Y") * mouseSensitivity;
         verticalRoatation = Mathf.Clamp(verticalRoatation, -mouseVertRange, mouseVertRange);
         
         Camera.main.transform.localRotation = Quaternion.Euler(-verticalRoatation,0 ,0);
         
         // Jumping
         if (characterController.isGrounded){
             verticalVelocity = 0;
             if(Input.GetButtonDown("Jump")){
                 verticalVelocity = jumpVelocity;
             }
         }
         
         // Movement
         forwardSpeed = Input.GetAxis("Forward / Backward") * movementSpeed;
         leftRightSpeed = Input.GetAxis("Left / Right") * movementSpeed;
         verticalVelocity += Physics.gravity.y * Time.deltaTime;
         movement = new Vector3(leftRightSpeed, verticalVelocity, forwardSpeed);
         movement = transform.rotation * movement;
         
         characterController.Move(movement * Time.deltaTime);
         
     }
 }
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
0
Best Answer

Answer by Owen-Reynolds · Jul 31, 2013 at 01:29 PM

This involves some of the trickier parts of Unity. CharacterControllers really hate having childed colliders, and adding rigidbodies, etc... just makes them madder. Lots of questions here about players carrying stuff.

Simplest might be to not child to cube and put it on a spring (attached to an empty in front of the player.) Attaching a spring during play isn't super-easy, but UAnswers has some things on it. Could start with one cube attached, just to see how it looks. Adjusting the spring settings to look good is also not super-easy.

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 Kyieon · Jul 31, 2013 at 06:05 PM 0
Share

Thank you. It was relatively easy to get up and running but as you said it is a pain to configure.

avatar image
0

Answer by RPGstandard · Jul 31, 2013 at 12:53 PM

you could create a function that determines the cubes velocity, and then applies a degraded amount of velocity to the cube in a direction based on the angle of impact.

Comment
Add comment · Show 7 · 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 Kyieon · Jul 31, 2013 at 12:54 PM 0
Share

could you give me an example of this?

avatar image RPGstandard · Jul 31, 2013 at 01:08 PM 0
Share

http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.OnCollisionEnter.html

plus

http://docs.unity3d.com/Documentation/ScriptReference/Force$$anonymous$$ode.VelocityChange.html

plus

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.SmoothDampAngle.html

combining these elements together should give you a nice script that will bounce the object off of any other object based on its angle and speed of impact

avatar image Kyieon · Jul 31, 2013 at 01:19 PM 0
Share

Will this also work if the rigidbody cube is parented to the character controller?

avatar image RPGstandard · Jul 31, 2013 at 01:26 PM 0
Share

The object should be unparented from the character controller at the point of impact , it might still work otherwise, but I can't predict how it's going to behave. You should try it out and see, I'm rather curious now

avatar image Kyieon · Jul 31, 2013 at 01:34 PM 0
Share

I'll get to work. I will tell you how I get on.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

" You should never have a parent and child rigidbody together " ? 2 Answers

Is it possible to prevent parent rigidbody physics from effecting children rigidbodies? 1 Answer

Make a simple tree 1 Answer

Child Gameobjects are not fixed to the parent when using physics 0 Answers

Child rigidbody acting strange when moving parent. 0 Answers


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