- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
Zombee · Jun 24, 2014 at 11:30 AM ·
camera-movement
Camera movement issue
Hello everyone,
I am trying for a little time to create a FPS view on a gameObject that can walk on wall. And I can't figure a good way to do it.
Here is the code:
var moveSpeed: float = 20; // move speed
var lerpSpeed: float = 10; // smoothing speed
var gravity: float = 10; // gravity acceleration
var isGrounded: boolean;
var deltaGround: float = 0.2; // character is grounded up to this distance
var jumpSpeed: float = 5; // vertical jump initial speed
var jumpRange: float = 5; // range to detect target wall
private var surfaceNormal: Vector3; // current surface normal
private var myNormal: Vector3; // character normal
private var distGround: float; // distance from character position to ground
private var jumping = false; // flag "I'm jumping to wall"
private var isTop = false;
private var PoV: Camera;
function Start(){
myNormal = Vector3.up; // normal starts as character up direction
rigidbody.freezeRotation = true; // disable physics rotation
// distance from transform.position to ground
distGround = collider.bounds.extents.y - collider.center.y;
PoV = transform.FindChild("Camera").gameObject.camera;
Debug.Log("Pov rotation = " + PoV.transform.rotation);
}
function FixedUpdate(){
// apply constant weight force according to character normal:
rigidbody.AddForce(-gravity*rigidbody.mass*myNormal);
}
function Update() {
// jump code - jump to wall or simple jump
if (jumping) return; // abort Update while jumping to a wall
if (Input.GetButtonDown("Jump")) { // jump pressed:
var hit: RaycastHit;
var ray = PoV.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit, jumpRange)) { // wall ahead?
JumpToWall(hit.point, hit.normal); // yes: jump to the wall
}
else if (isGrounded) { // no: if grounded, jump up
rigidbody.velocity += jumpSpeed * myNormal;
}
}
// Moving forward and backward
var forwardVector = PoV.transform.forward;
//forwardVector.y = 0;
forwardVector *= Input.GetAxis("Vertical");
transform.Translate(forwardVector*Time.deltaTime*moveSpeed);
// Moving left and right
var lateralVector = PoV.transform.right;
//lateralVector.y = 0;
lateralVector *= Input.GetAxis("Horizontal");
transform.Translate(lateralVector*Time.deltaTime*moveSpeed);
// update surface normal and isGrounded:
ray = Ray(transform.position, -myNormal); // cast ray downwards
if (Physics.Raycast(ray, hit)){ // use it to update myNormal and isGrounded
isGrounded = hit.distance <= distGround + deltaGround;
surfaceNormal = hit.normal;
}
else {
isGrounded = false;
// assume usual ground normal to avoid "falling forever"
surfaceNormal = Vector3.up;
}
// ! \ C'est içi que la camera rotate, il faut en changer le sens
myNormal = Vector3.Lerp(myNormal, surfaceNormal, lerpSpeed*Time.deltaTime);
// find forward direction with new myNormal:
var myForward = Vector3.Cross(transform.right, myNormal);
// align character to the new myNormal while keeping the forward direction:
var targetRot = Quaternion.LookRotation(myForward, myNormal);
Debug.Log("LookRotation = " + targetRot);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRot, lerpSpeed*Time.deltaTime);
}
function JumpToWall(point: Vector3, normal: Vector3){
// jump to wall
jumping = true; // signal it's jumping to wall
rigidbody.isKinematic = true; // disable physics while jumping
var dstPos = point + normal * (distGround + 0.5); // will jump to 0.5 above wall
var myForward = Vector3.Cross(transform.right, normal);
var dstRot = Quaternion.LookRotation(myForward, normal);
Debug.Log("LookRotation2 = " + dstRot);
for (var t: float = 0.0; t < 1.0; ){
t += Time.deltaTime;
transform.position = Vector3.Lerp(transform.position, dstPos, t);
transform.rotation = Quaternion.Slerp(transform.rotation, dstRot, t);
yield; // return here next frame
}
PoV.transform.rotation = transform.rotation;
Pov.transform.forward = transform.forward;
myNormal = normal; // update myNormal
rigidbody.isKinematic = false; // enable physics
jumping = false; // jumping to wall finished
}
With this code firstly I can't prevent the player to move upward. And secondly when I walk on a wall or on the ceiling the player movement are not align on the camera direction.
I don't know if I am understandable...
Thanks all.
Comment
Your answer
Follow this Question
Related Questions
Trouble with logic related to my camera smoothing 0 Answers
Rotate Camera to the object 0 Answers
Multitouch problem 0 Answers
Camera movement doesn't work in Unity (but does in builds) 0 Answers
How move camera without move selection? 0 Answers