Gravity and collision stopped working on player

Hi all

I have a simple cube that has mass, gravity enabled, contant force and a box collider.

This all worked as expected. The cube fell to the floor and skidded along the top in the direction of the contant force.

Now that I have added a script to the cube that updates its position and rotation every frame, gravity, contant force and collision no longer works.

Do I have to program all these things myself? Or is there a way to scipt an objects motion while keeping the physics?

Thanks for any info.

If you want it to move with physics (forces) then you need to use forces to move it and not overwriting the data. Setting the object’s position manually isn’t giving any leeway to the physics engine- it’s literally saying “STAY THERE!”. Look into the Rigidbody functions for ways to move your object around, or just look around for tutorials on Rigidbodies and/or Physics.

I see

Thanks for that.

Does this also apply to the box collider?

If my Y position gets below the floor my object passes straight through the floor.

I wouldn’t mind writing my own character controller and saying goodbye to the physics engine but I don’t want to write collision detection code.

Is that possible?

Thanks again

You can remove the rigidbody from the object if you have a rigidbody on the things you want it to collide against- the point is that one of the two has to have a rigidbody or collision messages just won’t happen. That said, gravity won’t affect the character anymore without a rigidbody, and you’ll have to manually write the collision detection code to stop your character from passing through objects.

Thanks Lysander

Still a bit unsure about it all.

Do most people use rigidbody for their main character?

I don’t see how I can use user input to control the main character while refraining from setting rotation and location of the main character so that all the physics can be applied.

Most people use a rigidbody when they want things like gravity and such, yes. In general, pressing the “forward” button will apply a force to the object in that direction, while pressing left or right will rotate the object clockwise or counterclockwise while the button is held, but really you can do it however you want.

There are a million ways to write a character controller, of course, and the one that I’m using for my current project is a click-to-move one that I created from scratch myself, but even that uses the physics engine- but only for the “y” position mostly. You can still move a character around manually and benefit from gravity and such if you only allow the input to alter the X and Z positions while ignoring Y, so perhaps you should try that.

Definitely check out the CharacterController included in the StandardAssets in Unity. You may also want to look at this: http://forum.unity3d.com/threads/charactermotor-fpsinputcontroller-platforminputcontroller-in-c.64378/

Try not to reinvent the wheel- there are dozens of great controllers out there already, and you can spend your time on things that are more specific to your game rather than going out of your way to make a new one.

Thanks man

Well I have tried moving my character with adding forces to my rigid body but I have ended up with something that is impossible to control.

Basically, my character at this point is a rectangle with a camera child that follows behind it.

When I tilt my phone left, the rectangle should rotate left up until 45 degrees, likewise to the right.

I created this behaviour with ease with the code below but it obviously breaks the physics.

Is there any possible way to implement this behaviour with physics?

private Quaternion localRotation;
 private float speed = 5.0f; 
 double accY;
 Vector3 position;

 // Use this for initialization
 void Start () {
 localRotation = transform.rotation;
 position = transform.position;
 }
 
 // Update is called once per frame
 void Update () {
 // find speed based on delta
 float curSpeed = Time.deltaTime * speed;

 accY = (Input.acceleration.z + 0.5) * 2;
 
 // first update the current rotation angles with input from acceleration axis
 localRotation.z -= Input.acceleration.x * curSpeed;
 localRotation.x -= (float)accY * curSpeed;

 transform.position = position;

 //Clamping Code
 if (localRotation.z >= 0.35f)
 localRotation.z = 0.35f;

 if (localRotation.z <= -0.35f)
 localRotation.z = -0.35f;

 if (localRotation.x >= 0.45f)
 localRotation.x = 0.45f;
 
 if (localRotation.x <= -0f)
 localRotation.x = -0f;

 transform.rotation = localRotation;
}

I tried AddRelativeTorque with tilt inputs and it behaved like an out of control seasaw that would barrel roll.

Most character controllers don’t actually use force based physics. In most games the player expects more control then this give them.

You can set the ridgidbody up as kinematic and the colliders as trigger. This allows you to do all of the movement via code, but still let the engine detect collisions for you.

As to your original position, you can use physics and direct manipulation of an objects transform together just fine as long as you add to the position each frame, rather then setting it.

Yeah, mine apparently doesn’t use a rigidbody like I thought anyways- it uses the CharacterMotor script to simulate/recreate most of the same things, like gravity and collisions. Nearly two days now without sleep and I’m apparently not dealing with it very well, although to be fair I haven’t actually touched any of the low-level controller scripts in my game in well over a month now. My apologies, in any case.

Absolutely must go pass out now.

lol

Well that makes more sense. I just spent 2 hours trying to rotate quaternions, mind blown :stuck_out_tongue:

I actually managed to update my rotation as needed with:

rb.rotation = Quaternion.Euler(rb.rotation.eulerAngles + new Vector3(0f, 0f, localRotation.z));

But have no idea how to clamp the angles so it doesn’t oversteer. Need a degree in math to figure that out :stuck_out_tongue:

Will go the custom controller route with kinematic option.
Thanks Mormon