Commit 05882b84 authored by kgingras's avatar kgingras
Browse files

smoother Character Controller

parent 949595de
fileFormatVersion: 2
guid: 4aa19d95d8173e847800d660d21da970
TextureImporter:
fileIDToRecycleName: {}
serializedVersion: 2
mipmaps:
mipMapMode: 0
enableMipMap: 1
linearTexture: 0
correctGamma: 0
fadeOut: 0
borderMipMap: 0
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: .25
normalMapFilter: 0
isReadable: 0
grayScaleToAlpha: 0
generateCubemap: 0
cubemapConvolution: 0
cubemapConvolutionSteps: 8
cubemapConvolutionExponent: 1.5
seamlessCubemap: 0
textureFormat: -3
maxTextureSize: 2048
textureSettings:
filterMode: -1
aniso: -1
mipBias: -1
wrapMode: 1
nPOTScale: 1
lightmap: 0
rGBM: 0
compressionQuality: 50
allowsAlphaSplitting: 0
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: .5, y: .5}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
alphaIsTransparency: 0
textureType: -1
buildTargetSettings: []
spriteSheet:
sprites: []
spritePackingTag:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: efcf604c3a25bf940b9644440b847e8f
TextureImporter:
fileIDToRecycleName: {}
serializedVersion: 2
mipmaps:
mipMapMode: 0
enableMipMap: 1
linearTexture: 0
correctGamma: 0
fadeOut: 0
borderMipMap: 0
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: .25
normalMapFilter: 0
isReadable: 0
grayScaleToAlpha: 0
generateCubemap: 0
cubemapConvolution: 0
cubemapConvolutionSteps: 8
cubemapConvolutionExponent: 1.5
seamlessCubemap: 0
textureFormat: -3
maxTextureSize: 2048
textureSettings:
filterMode: -1
aniso: -1
mipBias: -1
wrapMode: 1
nPOTScale: 1
lightmap: 0
rGBM: 0
compressionQuality: 50
allowsAlphaSplitting: 0
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: .5, y: .5}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
alphaIsTransparency: 0
textureType: -1
buildTargetSettings: []
spriteSheet:
sprites: []
spritePackingTag:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 942a24a67e4303f41b6f89fcc0e02456
TextureImporter:
fileIDToRecycleName: {}
serializedVersion: 2
mipmaps:
mipMapMode: 0
enableMipMap: 1
linearTexture: 0
correctGamma: 0
fadeOut: 0
borderMipMap: 0
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: .25
normalMapFilter: 0
isReadable: 0
grayScaleToAlpha: 0
generateCubemap: 0
cubemapConvolution: 0
cubemapConvolutionSteps: 8
cubemapConvolutionExponent: 1.5
seamlessCubemap: 0
textureFormat: -3
maxTextureSize: 2048
textureSettings:
filterMode: -1
aniso: -1
mipBias: -1
wrapMode: 1
nPOTScale: 1
lightmap: 0
rGBM: 0
compressionQuality: 50
allowsAlphaSplitting: 0
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: .5, y: .5}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
alphaIsTransparency: 0
textureType: -1
buildTargetSettings: []
spriteSheet:
sprites: []
spritePackingTag:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 85f7e8a396aa40545a6748bc184a7a58
TextureImporter:
fileIDToRecycleName: {}
serializedVersion: 2
mipmaps:
mipMapMode: 0
enableMipMap: 1
linearTexture: 0
correctGamma: 0
fadeOut: 0
borderMipMap: 0
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: .25
normalMapFilter: 0
isReadable: 0
grayScaleToAlpha: 0
generateCubemap: 0
cubemapConvolution: 0
cubemapConvolutionSteps: 8
cubemapConvolutionExponent: 1.5
seamlessCubemap: 0
textureFormat: -3
maxTextureSize: 2048
textureSettings:
filterMode: -1
aniso: -1
mipBias: -1
wrapMode: 1
nPOTScale: 1
lightmap: 0
rGBM: 0
compressionQuality: 50
allowsAlphaSplitting: 0
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: .5, y: .5}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
alphaIsTransparency: 0
textureType: -1
buildTargetSettings: []
spriteSheet:
sprites: []
spritePackingTag:
userData:
assetBundleName:
assetBundleVariant:
......@@ -50,27 +50,24 @@ public class CapsuleController : MonoBehaviour {
Ray ray;
RaycastHit hit;
// if (Input.GetKeyUp(KeyCode.B)){ // jump pressed:
//
//// if (Physics.Raycast(transform.position, -myNormal, out hit, jumpRange)){ // wall ahead?
//// // JumpToWall(hit.point, hit.normal); // yes: jump to the wall
//// }
// if (isGrounded){ // no: if grounded, jump up
// Debug.Log("jump");
// isGrounded = false;
////
//// Vector3 velocity = rb.velocity;
////
//// velocity += jumpSpeed * myNormal;
// rb.AddForce(jumpSpeed * myNormal, ForceMode.Impulse);
//
// }
// }
RaycastHit forwardHit;
if (Physics.Raycast(transform.position, -myNormal, out hit, Mathf.Infinity)){ // use it to update myNormal and isGrounded
if(Physics.Raycast(transform.position, myForward, out forwardHit, 1f)){
if(forwardHit.distance < hit.distance){
isGrounded = forwardHit.distance <= distGround + deltaGround;
surfaceNormal = forwardHit.normal;
}
else{
isGrounded = hit.distance <= distGround + deltaGround;
surfaceNormal = hit.normal;
}
}
else{
isGrounded = hit.distance <= distGround + deltaGround;
surfaceNormal = hit.normal;
// thirdPersonScript.newNormal = surfaceNormal;
}
}
else {
isGrounded = false;
......@@ -81,85 +78,73 @@ public class CapsuleController : MonoBehaviour {
myNormal = Vector3.Lerp (myNormal, surfaceNormal, lerpSpeed * Time.deltaTime);
} else {
myNormal = Vector3.Lerp (myNormal, Vector3.up, lerpSpeed * Time.deltaTime);
}
//adjust the rotation of the character based on the normal.
Quaternion targetRot = Quaternion.LookRotation(myForward, myNormal);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRot, lerpSpeed*Time.deltaTime);
GetMouseLook();
GetMovement();
// if (isGrounded) {
// AddGravity ();
// } else {
// AddRealGravity();
// }
}
void GetMovement(){
if (Input.GetKeyUp(KeyCode.B)){ // jump pressed:
// if (Physics.Raycast(transform.position, -myNormal, out hit, jumpRange)){ // wall ahead?
// // JumpToWall(hit.point, hit.normal); // yes: jump to the wall
// }
if (Input.GetKeyUp(KeyCode.Space)){ // jump pressed:
if (isGrounded){ // no: if grounded, jump up
Debug.Log("jump");
isGrounded = false;
//
// Vector3 velocity = rb.velocity;
//
// velocity += jumpSpeed * myNormal;
transform.position += myNormal*.05f;
rb.AddForce(jumpSpeed * myNormal, ForceMode.Impulse);
}
return;
}
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 targetVelocity = Vector3.zero;
Vector3 velocity = Vector3.zero;
Vector3 velocityChange = Vector3.zero;
if(isGrounded){
//forward and right adjusted based on the normal of the surface
myForward = Vector3.Cross(transform.right, myNormal);
myRight = Vector3.Cross(-transform.forward, myNormal);
//adjust the rotation of the character based on the normal.
Quaternion targetRot = Quaternion.LookRotation(myForward, myNormal);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRot, lerpSpeed*Time.deltaTime);
Vector3 targetVelocity = myForward * v + myRight * h;
///add gravity here
// if (isGrounded) {
// targetVelocity += -gravityForce*myNormal;
// }
targetVelocity = myForward * v + myRight * h;
targetVelocity *= walkSpeed;
if(!isGrounded) {
targetVelocity += -gravityForce*Vector3.up;
}
else{
if(h == 0 && v==0){
}
else{
targetVelocity += -runGravityForce*myNormal;
}
velocity = rb.velocity;
velocityChange = (targetVelocity - velocity);
rb.AddForce(velocityChange, ForceMode.VelocityChange);
}
else{
myForward = Vector3.Cross(transform.right, myNormal);
myRight = Vector3.Cross(-transform.forward, myNormal);
targetVelocity = myForward * v + myRight * h;
targetVelocity *= (walkSpeed / 2);
targetVelocity.y -= gravityForce *Time.deltaTime;
rb.velocity+=targetVelocity;
}
Vector3 velocity = rb.velocity;
Vector3 velocityChange = (targetVelocity - velocity);
rb.AddForce(velocityChange, ForceMode.VelocityChange);
//call thirdperson move
// Debug.Log (v + " vel:" +rb.velocity.magnitude + " change: " + velocityChange + " target: " + targetVelocity.magnitude);
thirdPersonScript.Move (targetVelocity, false, false, yRot);
thirdPersonScript.Move (targetVelocity, false, !isGrounded, yRot);
}
void AddGravity(){
......
......@@ -33,13 +33,13 @@ public class ThirdPersonAnimator : MonoBehaviour {
m_Animator = GetComponent<Animator>();
CCscript = GameObject.Find("Capsule").GetComponent<CapsuleController>();
//m_Rigidbody = GetComponent<Rigidbody>();
//m_Capsule = GetComponent<CapsuleCollider>();
//m_CapsuleHeight = m_Capsule.height;
//m_CapsuleCenter = m_Capsule.center;
m_Rigidbody = GetComponent<Rigidbody>();
m_Capsule = GetComponent<CapsuleCollider>();
m_CapsuleHeight = m_Capsule.height;
m_CapsuleCenter = m_Capsule.center;
//m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
//m_OrigGroundCheckDistance = m_GroundCheckDistance;
m_OrigGroundCheckDistance = m_GroundCheckDistance;
}
// Update is called once per frame
......@@ -106,4 +106,6 @@ public class ThirdPersonAnimator : MonoBehaviour {
m_Animator.speed = 1;
}
}
}
fileFormatVersion: 2
guid: f035c724f5cf8c347b4ce85e2f8cc94d
folderAsset: yes
timeCreated: 1454808404
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
No preview for this file type
fileFormatVersion: 2
guid: 6cdacfda6cb5297428d7df22a96a244d
timeCreated: 1454808404
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 3c7e6af9b719b8a4a85054a7f5754c1b
timeCreated: 1454808404
licenseType: Free
TextureImporter:
fileIDToRecycleName:
8900000: generatedCubemap
serializedVersion: 2
mipmaps:
mipMapMode: 0
enableMipMap: 1
linearTexture: 0
correctGamma: 0
fadeOut: 0
borderMipMap: 0
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: .25
normalMapFilter: 0
isReadable: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 1
cubemapConvolutionSteps: 8
cubemapConvolutionExponent: 1.5
seamlessCubemap: 1
textureFormat: -1
maxTextureSize: 2048
textureSettings:
filterMode: 2
aniso: 0
mipBias: 0
wrapMode: 1
nPOTScale: 1
lightmap: 0
rGBM: 0
compressionQuality: 100
allowsAlphaSplitting: 0
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: .5, y: .5}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
alphaIsTransparency: 0
textureType: 3
buildTargetSettings: []
spriteSheet:
sprites: []
spritePackingTag:
userData:
assetBundleName:
assetBundleVariant:
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment