Search Unity

Camera to start moving on collision of object.

Discussion in 'Scripting' started by DarkNeo, Apr 30, 2014.

  1. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Hi there,

    I have my camera moving script, basically What I am trying to do, is on collision of the character on another object I need the following camera script to run, basically the camera to starts moving up.

    Code (csharp):
    1. var Speed = 1;
    2.  
    3. function Update ()
    4.  
    5. {  
    6.     transform.Translate(Vector3(0,1,0) * Time.deltaTime);
    7.  
    8. }

    Here is a script that I have been working on for OnCollisionEnter and I can't get it to run. anyone have any suggestions? or recommend another way to approach this?

    Code (csharp):
    1. var Speed = 1;
    2.  
    3. var touched = new HashSet.<GameObject>();
    4.  
    5.  
    6. function OnCollisionEnter(collision : Collision) {
    7.  
    8.     if (collision.gameObject.tag == ("Player")  touched.Add(collision.gameObject))
    9. {
    10.    
    11.     transform.Translate(Vector3(0,1,0) * Time.deltaTime);
    12.        
    13.  
    14.     }
    15.  
    16. }

    Thanks :)
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. transform.Translate()
    3.  
    This is "teleporting" to a new location each frame, it doesn't interact with the physic engine and therefore doesn't create any collisions. If you want to use the in built physics collisions you need to move by applying forces to the rigidbody. Or you can use translate and check for collisions in your own scripts.

    Plenty of examples of both methods around here...