- Home /
Rigidbody not being effected by unity's gravity.
Hello, I'm new to unity so forgive my newbish question, anyway i have a character, rigidbody attached with capsule collider, kinematic is turned off and gravity is turned on mass is set to 100 and drag is at 1 but when i move the character over to and edge of terrain (not the end of the terrain object but a "cliff" in the terrain) the character only moves down the y axis at a rate of like .001 per frame. I feel that my rotation script is to blame but i cannot figure out how to fix this problem.
heres my movement script
public class Movement: MonoBehaviour {
public Transform lookingObj;
Plane ground;
void Start () {
}
void Update () {
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);
}
}
}
all the other aspects work fine but if i need to rewrite this code to make the gravity work i will do that.
Thanks a bunch! ~ Soos
Answer by JusticeAShearing · Jul 12, 2014 at 03:48 PM
I'm not a C# coder, but I have a Java Script that should help you. It requires a character controller, and I've cut out the animations bit for you. It has gravity applied to the character. It also comes with movement, rotation and jumping. I think that character controllers come with Unity. Either that or I got it for nothing off of the Asset Store.
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
var rotateSpeed : float = 3.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
//Grounded, so recalculate
//Move directly from axes
moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
//Rotation Code
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
//Apply Gravity
moveDirection.y -= gravity * Time.deltaTime;
//Move Controller
controller.Move(moveDirection * Time.deltaTime);
}
Thank you for your response, unfortunately this script doesn't help me, my movement only goes forward where the rotation of the mouse controls the direction that the character is facing. I attempted to apply the gravity using a vector3 and =- gravity but to no avail. I'm trying to understand why the standard unity gravity on rigidbodies isn't taking effect either.
I have recently converted from this movement script into one just like that which you have described. I will try to help you further, now that I have the resources. I also have no rigidbody, but may do later.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Rigidbody unaffected by standard unity gravity and script gravity 3 Answers
Rotation of object 0 Answers
Fire Bullet To Mouse Position Problem 2 Answers
Make character walk on walls defying gravity in a 2D Game 1 Answer