Search Unity

FPS camera collision

Discussion in 'Editor & General Support' started by RockHound, Apr 14, 2006.

  1. RockHound

    RockHound

    Joined:
    Apr 14, 2006
    Posts:
    132
    I would like a third person controller with the camera following slightly behind, but if my player backs into a wall, the camera's follow distance would shorten accordingly.

    How is the best way to implement this? Should I put another CharacterController on the camera? Could I simply use the Raycast method of the Physics class?

    It seems that this would be a common issue. Is there a thread that discusses this particular camera behavior?

    Thanks for any suggestions.
     
  2. Sync1B

    Sync1B

    Joined:
    Sep 13, 2005
    Posts:
    561
    I think raycasting in all directions around your camera, and changing it's position acordingly is the best way to go. I will have to do a similar thing soon.

    Hope that helps.

    Bill
     
  3. aigam

    aigam

    Joined:
    Dec 31, 2005
    Posts:
    170
    I think that the best is use a raycast from the player to the camera and put the camera in the:
    Max distance or Collision distance

    I'm now implementing one of this ^^.
     
  4. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    I use a phyisics based camera for my racing game. The camera has a rigidbody and collider on it so it never goes inside objects. Here is the script:

    Code (csharp):
    1.  
    2.  
    3. var target : Transform;
    4. var body : GameObject;
    5.  
    6. var distance = 0.000;
    7. var normalTargetDistance = 7.00;
    8. var targetDistance = 7.00;
    9.  
    10. var forceFactor = 0.00;
    11.  
    12. var inwardForce = 160.00;
    13. var upwardForce = 40.00;
    14. var lookDamping = 4.80;
    15. var normalDrag = 6;
    16.  
    17. var airMode = false;
    18.  
    19. rigidbody.freezeRotation = true;
    20.  
    21. function FixedUpdate()
    22. {
    23.     script = body.GetComponent(Car);
    24.     if(script) airMode = script.airMode;
    25.  
    26.     if(airMode)
    27.     {
    28.         targetDistance += Time.deltaTime * 4;
    29.         rigidbody.drag += Time.deltaTime * 2;
    30.     }
    31.     else
    32.     {
    33.         targetDistance = normalTargetDistance;
    34.         rigidbody.drag = normalDrag;
    35.     }
    36.     rotation = Quaternion.LookRotation(target.position - transform.position);
    37.     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * lookDamping);
    38.     transform.localEulerAngles.z = 0;
    39.  
    40.     distance = Vector3.Distance(transform.position, target.transform.position);
    41.  
    42.  
    43.     if(targetDistance < distance) forceFactor = distance - targetDistance;
    44.     if(targetDistance > distance) forceFactor = 0;
    45.  
    46.     rigidbody.AddRelativeForce (Vector3.forward * inwardForce * forceFactor * Time.deltaTime);
    47.     if(transform.localEulerAngles.x < 30) rigidbody.AddRelativeForce (Vector3.up * upwardForce * Time.deltaTime);
    48. }
    49.  
    50.  
    51.