- Home /
Rigidbody unaffected by standard unity gravity and script gravity
Hello, I have a movement script where the player rotates to where the mouse position's raycast and a created plane meet. The problem is that my character is unaffected by gravity regardless of how I try to create the force. The player moves forward and backward just fine as well as rotates perfectly. Kinematic is off, gravity is on, mass is set to 100 and drag is 1. If anyone can help me understand why the gravity isn't working it would help me figure out a way to fix it. Here's my movement Script:
public class Movement: MonoBehaviour {
public Transform lookingObj;
Plane ground;
public GameObject player;
public float gravity = 2000.0f;
public Vector3 moveDirection = Vector3.zero;
void Start () {
}
void Update () {
moveDirection = player.transform.position;
ground = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float dist;
if (ground.Raycast(ray, out dist)) {
Vector3 clickPoint = ray.GetPoint(dist);
Vector3 newClickPoint = new Vector3(clickPoint.x, clickPoint.y, clickPoint.z);
lookingObj.LookAt(newClickPoint);
}
moveDirection.y -= gravity * Time.deltaTime;
}
}
Note: Though I don't have much scripting experience, I feel that the character is rotating toward the plane which is created at the player's level, i believe this is what is causing the player to float without being affected by gravity. I am not using any character controllers from unity, just a regular modeled character.
Thanks and have a great one!
Answer by Anti-Social Fred · Jul 14, 2014 at 02:37 PM
Whenever I get problems like this it's always because of something silly where your "moveDirection" would be getting changed to something like 0,0,0. Have you debugged it in the method right before it's called? Are you sure that the Y-factor ISN'T 0?
The reason why I'm bringing this up is because I see that you change moveDirection at the end of your Update call, and then reset it at the beginning back to the player's position. That being said, I also don't see the player moving in this script which could cause this exact problem. I've had it happen to me in the past where one Update call would get messed up by another.
The last solution that I can think of is that your gravity float might not be correctly serialized in Unity, which means that even though it says 2000, the instance of the class might actually be holding a reference to 0. Just check the inspector; i'm sure that's not it, but that too is an issue that I had when I was first learning Unity.
Also, in terms of the normal Unity gravity... make sure that under your Rigidbody constraints you don't have Y-axis checked. Also, be sure that it's not Kinematic.
If he is using a rigidbody there's no point in updating the y position, gravity should be applied automatically, right?
Answer by Yofurioso · Jul 14, 2014 at 02:44 PM
It appears to me that you are creating a new plane each frame on your players position (assuming this movement script is attached to your player). Since the planes y coordinate equals the players y coordinate on each frame (because it's been created on that position) the player will always be touching the plane (conviniently named 'ground'). Is there a reason to create the plane on each frame? Try commenting the line "ground = new Plane..." and just add a plane on the scene view.
Answer by Soos621 · Jul 14, 2014 at 07:48 PM
Is there just a better way to rotate my character with the mouse?
I am still under the impression that because the rotation is set to a raycast from the "ground" plane which is set to the the same position as the character; it is constantly moving the player to the y position of the plane. also, note that when I walk my character off a terrain-made cliff he will slowly move down the y axis at about .1 units. Suggesting that unity's gravity is technically pulling down on the character.
Basically I want the character to rotate his x and z axis to the mouse and ignore the y position altogether.
I have an idea, though I don't know exactly how I can script it, but basically if I could set a plane to the character's transform x and z and the plane's y value could be set to a downward raycast from the character to the "terrain" GameObject, which measures the distance, and adjusts the players transform y value. I don't know if this would work, but I'm having trouble finding much information about character following the mouse for directional changes. Any tutorials or references would be appreciated.
I do want the character to jump, but i figure the gravity thing seems more important.