Ok, I have a script that simulates a DeadSpace Third Person Camera. The script gets applied to the camera and the "target" is the player.
The script works fine, till i tried to apply "height". Some characters are bigger, some are smaller, therefore applying a value height makes sense. Sadly Physics.Linecast gives me strange results. I tried many things, but i can't find the error.
It seems like Physics.Linecast use the orgin model high, while camera and controls using the new height (origin height + variable height). Can someone help me out of this mess? Thx!
using UnityEngine;
using System.Collections;
public class Camera_DeadSpace : MonoBehaviour {
public Transform target;
public float distance = 2.0f;
public float height = 0.8f;
public float in_damping = 6.0f;
public float out_damping = 3.0f;
public float sensitivityX = 150F;
public float sensitivityY = 150F;
public float minimumY = -60F;
public float maximumY = 60F;
public float minDistance = 0.0f;
public float maxDistance = 4.0f;
public float smooth = 10.0f;
private bool showCrosshair = false;
private GUISkin skin = null;
private float rotationY = 0F;
private float rotationX = 0F;
private float currentDistance;
void Awake(){
currentDistance = distance;
}
// Update is called once per frame
void LateUpdate () {
Screen.showCursor = false;
Screen.lockCursor = true;
Vector3 position = target.position;
//HERE IS THE PROBLEM !!! IF YOU USE THIS LINE IT KILLS THE SCRIPT!!!
//position.y = height + target.position.y;
Vector3 transpos, targetrot;
Quaternion transrot;
if (!Input.GetButton("Aim")){
showCrosshair=true;
float wantedDistance = distance;
currentDistance = Mathf.Lerp (currentDistance, wantedDistance, out_damping * Time.deltaTime);
rotationX += Input.GetAxis("Mouse X") * sensitivityX * Time.deltaTime;
rotationY -= Input.GetAxis("Mouse Y") * sensitivityY * Time.deltaTime;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transrot = Quaternion.Euler(rotationY, rotationX, 0) ;
transpos = (Quaternion.Euler(rotationY, rotationX, 0)) * new Vector3(0.0f, 0.0f, -currentDistance) + position;
targetrot = new Vector3(0, rotationX, 0);
}
else{
showCrosshair=true;
float wantedDistance = 0;
currentDistance = Mathf.Lerp (currentDistance, wantedDistance, in_damping * Time.deltaTime);
rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX * Time.deltaTime;
rotationY -= Input.GetAxis("Mouse Y") * sensitivityY * Time.deltaTime;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transrot = Quaternion.Euler(rotationY, rotationX, 0) ;
transpos = (Quaternion.Euler(rotationY, rotationX, 0)) * new Vector3(0.0f, 0.0f, -currentDistance) + position;
targetrot = new Vector3(0, rotationX, 0);
}
Vector3 desiredCameraPos = transpos;
RaycastHit hit;
//float distance2 = 0;
if( Physics.Linecast( position, desiredCameraPos, out hit ) ){
print("hit " + hit.distance);
transpos = (Quaternion.Euler(rotationY, rotationX, 0)) * new Vector3(0.0f, 0.0f, -hit.distance) + position;
}
transform.rotation = transrot;
transform.position = transpos;
target.localEulerAngles = targetrot;
}
// Use this for initialization
void Start () {
}
}