Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Gravity and collision stopped working on player

Discussion in 'Scripting' started by ComputerBruce, Aug 8, 2015.

  1. ComputerBruce

    ComputerBruce

    Joined:
    Aug 6, 2015
    Posts:
    22
    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.
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    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.
     
  3. ComputerBruce

    ComputerBruce

    Joined:
    Aug 6, 2015
    Posts:
    22
    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
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    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.
     
  5. ComputerBruce

    ComputerBruce

    Joined:
    Aug 6, 2015
    Posts:
    22
    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.
     
  6. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    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/ch...ontroller-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.
     
  7. ComputerBruce

    ComputerBruce

    Joined:
    Aug 6, 2015
    Posts:
    22
    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?


    Code (csharp):
    1.  
    2. private Quaternion localRotation;
    3.  private float speed = 5.0f;
    4.  double accY;
    5.  Vector3 position;
    6.  
    7.  // Use this for initialization
    8.  void Start () {
    9.  localRotation = transform.rotation;
    10.  position = transform.position;
    11.  }
    12.  
    13.  // Update is called once per frame
    14.  void Update () {
    15.  // find speed based on delta
    16.  float curSpeed = Time.deltaTime * speed;
    17.  
    18.  accY = (Input.acceleration.z + 0.5) * 2;
    19.  
    20.  // first update the current rotation angles with input from acceleration axis
    21.  localRotation.z -= Input.acceleration.x * curSpeed;
    22.  localRotation.x -= (float)accY * curSpeed;
    23.  
    24.  transform.position = position;
    25.  
    26.  //Clamping Code
    27.  if (localRotation.z >= 0.35f)
    28.  localRotation.z = 0.35f;
    29.  
    30.  if (localRotation.z <= -0.35f)
    31.  localRotation.z = -0.35f;
    32.  
    33.  if (localRotation.x >= 0.45f)
    34.  localRotation.x = 0.45f;
    35.  
    36.  if (localRotation.x <= -0f)
    37.  localRotation.x = -0f;
    38.  
    39.  transform.rotation = localRotation;
    40. }
    41.  
    I tried AddRelativeTorque with tilt inputs and it behaved like an out of control seasaw that would barrel roll.
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  9. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    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.
     
  10. ComputerBruce

    ComputerBruce

    Joined:
    Aug 6, 2015
    Posts:
    22
    lol

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

    I actually managed to update my rotation as needed with:

    Code (csharp):
    1.  
    2. rb.rotation = Quaternion.Euler(rb.rotation.eulerAngles + new Vector3(0f, 0f, localRotation.z));
    3.  
    But have no idea how to clamp the angles so it doesn't oversteer. Need a degree in math to figure that out :p

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