I am Having Following Script for my character controller to move around and simple jump…
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
When I use the following code…I am having very less gravity in compare of above code…
using UnityEngine;
using System.Collections;
public class UnityCC : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
print(moveDirection);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
can anyone please tell me why this is happening…and by just removing jump button and if condition cause this problem???
what do you mean by your having very less gravity? From the second code your posted you never jump up. So you should always be on the ground?
i just manually enter the transform position 's y in inspector in both of above cases…
and i dont know why 1. case having perfact gravity and but not other one
I’d have to see the CharacterController script’s move function. It could be that if isGrounded is set it doesn’t care about gravity. And if you manually put yourself in the air… isGrounded might stay true.
ok…i will post all my code in hour
this is my character controller movement code…when its at height and it falls…it falls very slowly…
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class FPS_Controller : MonoBehaviour {
[System.Serializable]
public class Script_Resources
{
[Header("Script Resources:")]
public Mobile_Joystick Joystick;
public Mobile_Touchpad Touchpad;
public Camera FPS_Camera;
public Button Stance_Button;
}
public Script_Resources Resources;
[System.Serializable]
public class Player_Settings
{
[Header("FPS Player Setting:")]
public Vector3 Stand_Size;
public Vector3 Crouch_Size;
public enum Stances
{Stand,Crouch};
public Stances Current_Stance;
public float Gravity;
public float Walk_Speed;
public float Max_Climb_Angle;
public string Slide_Object_Tag;
public float Slide_Speed;
public bool InAir_Controll;
public float InAir_Speed_Magnitude=0.3f;
}
public Player_Settings FPS_Settings;
//Joystick and touchpad variables
private Vector2 Joystick_Vector;
private Vector3 Movement_Vector;
private Vector2 Touchpad_Vector;
private Vector3 Camrotation_Vector;
//Access Private Varibles
private CharacterController Character_Controller;
//other private variables
public bool Is_Controllabel;
public bool Is_Sliding;
public bool Is_Grounded;
//ray cast up while crouching to stand for obsticals over head
private Vector3 Ray_UP;
private Vector3 Before_Air_Pos;
private RaycastHit Ray_Down;
private Vector3 Ray_Down_Position;
void Start () {
Character_Controller = GetComponent<CharacterController> ();
Character_Controller.slopeLimit = FPS_Settings.Max_Climb_Angle;
}
void Update () {
Joystick_Vector = Resources.Joystick.Joystick_Input ();
Touchpad_Vector = Resources.Touchpad.Touchpad_Input ();
Resources.FPS_Camera.transform.Rotate (Vector3.left*Touchpad_Vector.y);
transform.Rotate ( Touchpad_Vector.x * Vector3.up);
if (Is_Grounded) {
Check_Sliding ();
if (Is_Controllabel) {
Movement_Vector = new Vector3 (Joystick_Vector.x, 0.0f, Joystick_Vector.y);
Movement_Vector *= FPS_Settings.Walk_Speed;
Movement_Vector = transform.TransformDirection (Movement_Vector);
}
if (Is_Sliding) {
Ray_Down_Position = Ray_Down.normal;
Movement_Vector = new Vector3 (Ray_Down_Position.x, -Ray_Down_Position.y, Ray_Down_Position.z);
Vector3.OrthoNormalize (ref Ray_Down_Position, ref Movement_Vector);
Movement_Vector *= FPS_Settings.Slide_Speed;
}
}
else if(!Is_Grounded) {
if(FPS_Settings.InAir_Controll)
{
Movement_Vector = new Vector3 (Joystick_Vector.x, 0.0f, Joystick_Vector.y);
Movement_Vector *= FPS_Settings.InAir_Speed_Magnitude;
Movement_Vector = transform.TransformDirection (Movement_Vector);
}
}
Movement_Vector.y -= FPS_Settings.Gravity*Time.deltaTime ;
Is_Grounded = (Character_Controller.Move (Movement_Vector * Time.deltaTime) & CollisionFlags.Below) != 0;
}
void Check_Sliding ()
{
if (Physics.Raycast(transform.position, -Vector3.up, out Ray_Down, Character_Controller.radius+Character_Controller.height))
Is_Sliding= (Ray_Down.collider.tag==FPS_Settings.Slide_Object_Tag && Vector3.Angle(Ray_Down.normal, Vector3.up) > FPS_Settings.Max_Climb_Angle) ? true : false;
Is_Controllabel=(Is_Sliding)?false:true;
}
void FixedUpdate()
{
if (FPS_Settings.Current_Stance == FPS_Controller.Player_Settings.Stances.Crouch) {
Ray_UP = transform.TransformDirection (Vector3.up);
Resources.Stance_Button.GetComponent<Button> ().interactable = (Physics.Raycast (transform.position, Ray_UP, FPS_Settings.Stand_Size.y)) ? false : true;
}
}
void OnControllerColliderHit (ControllerColliderHit hit) {
}
}
Ah I didn’t realize CharacterController was a unity Class
Have to look at it a bit.
What is your Gravity set to here?
Movement_Vector.y -= FPS_Settings.Gravity*Time.deltaTime ;
Is_Grounded = (Character_Controller.Move (Movement_Vector * Time.deltaTime) & CollisionFlags.Below) != 0;
Character Controller move states it does no Gravity at all. So you have to supply the gravity yourself.
still the same result…
i applied it 20 gravity…
Movement_Vector.y -= 20 * Time.deltaTime;
Is_Grounded = (Character_Controller.Move (Movement_Vector * Time.deltaTime) & CollisionFlags.Below) != 0;