我的相机不再留在我的角色了。

时间:2014-11-05 16:18:39

标签: c# unity3d

using UnityEngine;
using System.Collections;
/// <summary>
 /// Description of class#
 /// </summary>
[RequireComponent (typeof (BarsEffect))]
public class ThirdPersonCameraz : MonoBehaviour {
[SerializeField]
/// <summary>
/// The distance away from the character.
/// </summary>
private float distanceAway;
[SerializeField]
/// <summary>
/// The distance up away from the camera.
/// </summary>
private float distanceUp;
[SerializeField]
/// <summary>
/// How long it will take for the camera to go from one place, to where we are trying to place the camera.
/// </summary>
private float smooth;
[SerializeField]
/// <summary>
/// The reason that the follow is a transform is because the characters position is going to be updated using physics
/// calculations. since the character has a rigid body on him, the physics are a fixed update which is bad because it
/// doesnt get updated as often.
/// </summary>
private Transform followXForm;

private float widescreen = 0.2f;
private float targetingTime = 0.5f;


private Vector3 offset = new Vector3(0f,1.5f, 0f);

private Vector3 lookDir;
/// <summary>
/// target position is where we will be trying to position the camera.
/// </summary>
private Vector3 targetPosition;

private BarsEffect barEffect;
private CamStates camState=CamStates.Behind;

private Vector3 velocityCamSmooth= Vector3.zero;

private float camSmoothDampTime = 0.1f;
public enum CamStates
{
    Behind,
    FirstPerson,
    Target,
    Free
}



// Use this for initialization
void Start () {
    ///this will grab the Follow game object which is tagged as a player
    followXForm = GameObject.FindWithTag ("Player").transform;
    lookDir = followXForm.forward;

    barEffect = GetComponent<BarsEffect> ();
    if (barEffect == null)
    {
        Debug.LogError("Attach a widescreen BarsEffect script to the camera.", this);
                       }

}

// Update is called once per frame
void Update () {

}


void OnDrawGizmos()
{
}
/// <summary>
/// this function is good for making a camera because after you position all your objects
/// you can put ur camera code here and it will make sure everything lines up properly since it happens 
/// after update.
/// </summary>
void LateUpdate()
{
    // distance from the characters follow to the height.
    Vector3 characterOffset = followXForm.position + new Vector3 (0f, distanceUp, 0f);

    //determines the camera state
    //calculates the direction from camera to player, kill y and normalize to give a valid direction with unit magnitude.
    //
    if (Input.GetAxis ("Target") > 0.01f) {
                    barEffect.coverage = Mathf.SmoothStep (barEffect.coverage, widescreen, targetingTime);
                    camState = CamStates.Target;
            }
    else {
                    barEffect.coverage = Mathf.SmoothStep (barEffect.coverage, 0f, targetingTime);
                    camState = CamStates.Behind;
            }

    switch(camState)
    {
    case CamStates.Behind:
    ///setting the target position, were taking the Follow position and moving it up by a distance, and moving
    /// back by distance away based on the follow vector.
    /// Vector.up will always be (0,1,0)


    ///drawing the characters position to the up vector we created.
    Debug.DrawRay (followXForm.position, Vector3.up * distanceUp, Color.red);
    /// Drawing the characters position based on the away distance
    Debug.DrawRay(followXForm.position,-1f*followXForm.forward*distanceAway,Color.blue);
    lookDir = characterOffset - this.transform.position;
    lookDir.y = 0;
    lookDir.Normalize ();
    Debug.DrawRay (this.transform.position, lookDir, Color.green);
    targetPosition = characterOffset + followXForm.up * distanceUp - lookDir * distanceAway;
    ///drawing a line from the follow position to the target position.
    Debug.DrawRay(followXForm.position, targetPosition,Color.magenta);
        break;
    case CamStates.Target:
        lookDir=followXForm.forward;



        break;
        }
    targetPosition = characterOffset = followXForm.up * distanceUp - lookDir * distanceAway;



    CompensateForWalls (characterOffset, ref targetPosition);
    ///making a smooth transition between its current position and the position it wants to be in
    /// delta time is the time between frames. delta time * smooth is normalizing the camera between frames.
    smoothPosition(this.transform.position, targetPosition);

    ///make sure the camera is looking at the character/follow(gameobject)
    transform.LookAt(followXForm);

}
private void smoothPosition (Vector3 fromPos, Vector3 toPos)
{
    this.transform.position = Vector3.SmoothDamp (fromPos, toPos, ref velocityCamSmooth, camSmoothDampTime);
} 
private void CompensateForWalls(Vector3 fromObject, ref Vector3 toTarget)
{
            Debug.DrawLine (fromObject, toTarget, Color.cyan);
            //compensate for walls between cameraz
            RaycastHit wallHit = new RaycastHit ();
    if (Physics.Linecast (fromObject, toTarget, out wallHit)) 
    {
                    Debug.DrawRay (wallHit.point, Vector3.left, Color.red);
                    toTarget = new Vector3 (wallHit.point.x, toTarget.y, wallHit.point.z);
            }
    }

}

好的,所以这里发生的是输入后         打破;         case CamStates.Target:         lookDir = followXForm.forward;

    break;
    }
targetPosition = characterOffset = followXForm.up * distanceUp - lookDir *distanceAway;

我的相机不再留在角色上。程序是这样的,当我向下按下左触发器时,它应该跟随角色,但当我没有按住它时,相机是空闲的,角色可以在相机周围运行。我很困惑,为什么它不会工作。我正在追随一个统一的第三人称控制器,并希望能得到任何帮助!谢谢!

0 个答案:

没有答案