- Home /
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);
}
}
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.
Thank you. It was relatively easy to get up and running but as you said it is a pain to configure.
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.
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
Will this also work if the rigidbody cube is parented to the character controller?
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
Your answer
Follow this Question
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
Child Gameobjects are not fixed to the parent when using physics 0 Answers
Child rigidbody acting strange when moving parent. 0 Answers
Make a simple tree 1 Answer