Search Unity

Camera collision detection?

Discussion in 'Scripting' started by gore23, Jun 4, 2011.

  1. gore23

    gore23

    Joined:
    Jan 18, 2011
    Posts:
    324
    I want to make my camera move at the ball when the camera has a object in between it, or is in a wall and back when there isnt for example ( atleast when between).

    How would I go about doing this I have been trying to figure it out XD

    I have the balls camera at a angle aiming down at it so I assume I need to use local position to make it move at the angle i want (its faceing ( currently at the ball)

    I was also puzzled on how to figure out what the object is that is hit by a ray (new to rays)
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Easy, you use a Physics.Raycast from the player to the camera. If it hits something, then you subtract 1 unit for measure and place your camera there.

    Code (csharp):
    1.  
    2. var ray : Ray=Ray(target.position, target.position - transform.position);
    3. var hit : RaycastHit;
    4. if(Physics.Raycast(ray, hit))
    5. transform.position=ray.GetPoint(hit.distance-1);
    6.  
     
  3. gore23

    gore23

    Joined:
    Jan 18, 2011
    Posts:
    324
    what is the target in target.position ?

    I have it like this

    Code (csharp):
    1.  
    2.  
    3. var player = GameObject;
    4.  
    5. function Update () {
    6.  
    7.     var ray : Ray = Ray(player.Transform.Position, player.Transform.Position - transform.Position);
    8.     var hit : RaycastHit;
    9.    
    10.     if(Physics.Raycast(ray, hit)){
    11.         Camera.main.transform.position = ray.GetPoint(hit.distance-1);
    12.        
    13.     }
    14. }
    However on line 6 i have problems with the player which will be the ball and the commands to get his position to start the ray and to send it to the camera *script attached to* I tried lots combos of what commands put after the player but doesnt work .....
     
    Last edited: Jun 4, 2011
  4. gore23

    gore23

    Joined:
    Jan 18, 2011
    Posts:
    324
    Does anyone know why the

    var ray : Ray = Ray(player.Transform.Position, player.Transform.Position - transform.Position);


    does not work, errors with red mark on left in editor ?
     
    Last edited: Jun 4, 2011