- Home /
Loss of gravity and interation with box collider
I'm pretty much brand new to scripting, but I have been using Unity as a way to help me learn, I find having a visual result of my efforts makes retaining information easier.
Anyway I am attempting to create a basic character that will move along the x axis and I seem to have accomplished that, but somewhere along the line I have lost the characters ability to fall and interact with the object below it that has a box collider. Below is the script i am working with, can anyone see where i have gone wrong?
var speed = 1.0;
//This works fine, but lacks gravity.
function Update () { //var xPos = Input.GetAxis("Horizontal") Time.deltaTime speed; ...This is defunct script - ignore. var controller : CharacterController = GetComponent(CharacterController);
var right =transform.TransformDirection(Vector3.forward);
var xPos = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
controller.SimpleMove(right * xPos);
//The below line allows the character to move, however it also prevents //the character from falling onto the ground and staying there, he floats //through the air when no geomentry is immediately below him, slowly //succombing to the effects of gravity that are affected through the above lines, why?
transform.position.x += xPos;
var move = Vector3(xPos, 0.0, 0.0);
if (move != Vector3.zero) {
var rotation = transform.rotation;
rotation.SetLookRotation(move);
transform.rotation = rotation;
}
}
Only problem is now is that when my little guy runs up a hill when he turns to run back down it he instead floats along the x axis and slowly goes back down to the ground cube i have... Can i increase the gravity of my little guy?
Update - Tried suggestion, did not work, but through experimentation I have narrowed it down to transform.position.x += xPos ...I think it's that little ".x" that is causing the problem, but I don't know how to rephrase it without losing the movement of the character along the x axis.
Answer by Wollowizard · Apr 12, 2011 at 08:59 PM
add a rigidbody to it and it will get the effect your after. or add something like moveDirection.y -= gravity * (Time.deltaTime/2);
I attempted to implement your suggestion, but I couldn't get it to work, probably due to my almost total lack of coding, but I have managed to figure out what stops the character from plummeting back to the ground, see update.
Your answer
Follow this Question
Related Questions
Why can't I move along the Y axis when I shift gravity from the Y to Z axis? 1 Answer
Gravity switch 1 Answer
how to apply orbit to the player 2 Answers
Space Game (Noob Movement Question) 0 Answers