I have been successful in making my character fly in JavaScript:
if(Input.GetKey(KeyCode.RightControl))
transform.Translate(Vector3.up * flySpeed * Time.deltaTime);
He flies right through objects, including the ground, something related to collisions turning off for controllers when using translate, I think, any help? Ground is rigid body and box collider.
Secondly, I want to make my character jump. He goes up, and doesn’t come down.
I’ve tried this:
if(Input.GetKey(KeyCode.Space))
transform.Translate(Vector3.up * jumpSpeed * Time.deltaTime);
Physics.gravity=Vector3(0,-3000,0);
Don’t pay attention to the numbers, as I haven’t scaled things to the way I want them yet.
The gravity does not work.
I have kinematics turned on, because if I don’t, the character falls right through the floor.
Any help on fixing physics collisions/working with the kinematics to not make my character phase through objects would be appreciated.
Detail: Other than mouseLook, I am making my own character “motor” to move him around.
I have briefly read that I can manipulate my character in relation to the axis, although this seems a nuisance since I would have to update code for every block he walks on… And it seems like it would eat a lot of CPU compared to a simply “jump” that understands physics.
Please, please help. I wish the simple jump physics was built in without the complicated characterMotor, etc.
Here’s an update after some searching, seems like a good idea, only it’s not working:
if(Input.GetKey(KeyCode.LeftControl)&&controller.collisionFlags != CollisionFlags.Below)
transform.Translate(Vector3.up * -flySpeed * Time.deltaTime);
//to land the character without him going through the floor while flying down.
I have searched Unity and found this jumping script, which is spectacular: (keep reading after script)
varspeed : float = 6.0;
varjumpSpeed : float = 8.0;
vargravity : float = 20.0;
privatevarmoveDirection : Vector3 = Vector3.zero;
functionUpdate() {
varcontroller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
//Wearegrounded, sorecalculate
//movedirectiondirectlyfromaxes
moveDirection = Vector3(Input.GetAxis(“Horizontal”), 0,
Input.GetAxis(“Vertical”));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton (“Jump”)) {
moveDirection.y = jumpSpeed;
}
}
//Applygravity
moveDirection.y -= gravity * Time.deltaTime;
//Movethecontroller
controller.Move(moveDirection * Time.deltaTime);
this works fine, but now there is a new problem. After my character moves around a bit, at a random time, the character suddenly drops through the floor…… this script solved one problem and created another. Anyone know what is going wrong?
privatevarmoveDirection is actually private var moveDirection in the script. Don’t know why, but clipboard pasted it differently
Okay, I think I may have found the error. My gravity was set way too high, and Unity could not calculate the force fast enough to create the physics barrier of the ground. Until the character starts falling through the ground again, whoever reads this may consider this problem solved (at least temporarily) and I hope this helps. 