Hi,
I’m using the First person controller prefab.
I have a lake on a map with a box collider. When entering the collider with the “player”, OnTriggerEnter sets the gravity to 0.
The problem is, that I still can’t move down if I look down with the mouse and I can’t move up if I look up with the mouse. The player ist just moving forward and backward, instead of moving in the direction where I look.
I guess, that in the script below moveDirection.y has to be set somewhere, but I can’t figure out, what variables have to be used, to calculate the y-movement.
Can someone please help me with this?
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
// Touching Water Collider
function OnTriggerEnter ( other : Collider) {
if (other.gameObject.name == "WaterCollider") {
// Turn gravity off and reduce speed
gravity = 0;
speed = 20;
}
}
// Leaving Water Collider
function OnTriggerExit ( other : Collider) {
if (other.gameObject.name == "WaterCollider") {
// Restore gravity and speed
gravity = 20.0;
speed = 100;
}
}
function FixedUpdate() {
// not grounded = in water
if (!grounded) {
moveDirection.x = Input.GetAxis("Horizontal")*speed;
moveDirection.y = ?????????????????????????????????????
moveDirection.z = Input.GetAxis("Vertical")*speed;
}
// If grounded, zero out y component and allow jumping
else {
moveDirection = Vector3(Input.GetAxis("Horizontal")*speed, 0.0, Input.GetAxis("Vertical")*speed);
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
moveDirection = transform.TransformDirection(moveDirection);
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
grounded = (GetComponent(CharacterController).Move(moveDirection * Time.deltaTime) CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
Greetings!
Hi,
i managed to solve this:
Move the Script FPSWalker to a custom folder (e.g. “test” in the root). It must not be in the Script-Folder, so that MouseLook.cs gets compiled first.
With “public var mtest:MouseLook;” in FPSWalker.js, one can access the class MouseLook in the script MouseLook.cs. In the inspector, click on the now available variable “MTest” and choose the script “Main Camera (MouseLook)”.
Click on First Person Controller Prefab and in the inspector under MouseLook (Script) set axes to “MouseX”.
Click on Main Camera (within First Person Controller) and in the inspector under Mouselook (Script) set axes to “MouseY”.
In MouseLook.cs, change line 34 and 35 to:
public float rotationX = 0F;
public float rotationY = 0F;
in FPSWalker.js:
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var rotationjY = 0.000000;
public var mtest:MouseLook;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
private var rotation_flat:float; // Speed Y (-1 to 1)
private var in_water:boolean; // I for myself will add colision detection with a water box so that I still can use the variable grounded AND move in water AND move in midair while jumping (not included in this example, this example is only ground and water)
private var rotation_flat_x:float; // Speeed X (-1 to 1)
// Touching Water Collider
function OnTriggerEnter ( other : Collider) {
if (other.gameObject.name == "WaterCollider") {
// Turn gravity off and reduce speed
gravity = 0;
speed = 20;
}
}
// Leaving Water Collider
function OnTriggerExit ( other : Collider) {
if (other.gameObject.name == "WaterCollider") {
// Restore gravity and speed
gravity = 20.0;
speed = 100;
}
}
function FixedUpdate() {
// not grounded = in water
if (!grounded) {
moveDirection.x = Input.GetAxis("Horizontal")*speed;
rotation_flat = mtest.rotationY / 90;
if(rotation_flat < 0)
{
rotation_flat_x = 1 + rotation_flat;
}
else
{
rotation_flat_x = 1 - rotation_flat;
}
Debug.Log("rf: "+rotation_flat+" rfx: "+rotation_flat_x);
moveDirection.z = rotation_flat_x*Input.GetAxis("Vertical")*speed;
moveDirection.y = rotation_flat*(Input.GetAxis("Vertical")*speed);
}
// If grounded, zero out y component and allow jumping
else {
moveDirection = Vector3(Input.GetAxis("Horizontal")*speed, 0.0, Input.GetAxis("Vertical")*speed);
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
moveDirection = transform.TransformDirection(moveDirection);
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
grounded = (GetComponent(CharacterController).Move(moveDirection * Time.deltaTime) CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
[code]
its cool, but the problem is that whenever you are not on the ground you are “in water” so there should be a third type normal etc…
i thought u make the object with no gravity and u add force to it…or something like that