• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by laurienash · Jun 02, 2013 at 11:44 AM · instantiatescriptingbasicsfirst-person-controllerthirdpersoncontroller

Script that switches between first and third person controller

Hello - in my game I switch between first and third person. When the third person collides with a certain trigger - the third person controller is destroyed, and a new one is instantiated.

The problem I have is that once the new controller is instantiated, this script no longer works - switching between first and third person. (as of course I only assigned the first third person controller and camera in the hierarchy)

Is there a way of changing the script so that rather than assigning the third person controller and camera in the hierarchy, the script just references whichever third person controller is currently active?

(Sorry I'm not sure how exactly to phrase it - I'm still very new to scripting)

This is the script I'm using to switch between characters (JavaScript):

 #pragma strict
 
       var cam01 : GameObject; // first person camera
     var cam02 : GameObject; // third person camera
     var player01 : GameObject; //first person controller
     var player02 : GameObject; //third person controller
     var check;                 // New check-variable
  
     //start with first person active
     function Start() {
        cam01.gameObject.active = true; 
        cam02.gameObject.active = false; 
        player02.active = false;
        check = true;
     }
  
  
     function Update() {
     
     player01.transform.position = player02.transform.position;
  
      if (Input.GetKeyDown ("return")) {
        if(check) {
          cam01.gameObject.active = false; 
          cam02.gameObject.active = true; 
          player01.active = false;
          player02.active = true;
        }
        else {
          cam01.gameObject.active = true; 
          cam02.gameObject.active = false; 
          player01.active = true;
          player02.active = false;
        }
     check = !check;
     }

Thanks very much, Laurien

Comment

People who like this

0 Show 18
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ExTheSea · Jun 02, 2013 at 12:31 PM 1
Share

You could assign the player02 variable after you instantiate it.

Pseudo Code:

 var newthirdpersoncontroller = Instantiate(....);
 var script : TheScript = playerGO.GetComponent(TheScript);
 script.player02 = newthirdpersoncontroller;
avatar image ExTheSea · Jun 02, 2013 at 04:03 PM 1
Share

No problem. I forgot. you also have to assign the cam02 variable when you instantiate the new one.

So you also have to do:

 script.cam02 = CameraofnewPrefab; //CameraofnewPrefab should be assigned with the Camera of the new ThirdPersonController if there is one.
avatar image ExTheSea · Jun 02, 2013 at 05:34 PM 1
Share

Is it possible that in the edge of the terrain there is a active Camera? I'm guessing that in the time between destroying the old Camera and instantiating the new one it automatically changes to the next active camera.

I'm gonna convert one of my comments to an answer so that you can accept it to mark the question as solved if it is.

avatar image ExTheSea · Jun 03, 2013 at 03:54 PM 1
Share

That sounds weird. There has to be a camera there either a static one or it's instantiating one. Maybe it's the third person/First Person camera? Could you check your scene hirarchy/scene view when this happens to see which camera or if there is a camera where the game view points from.

To which Gameobject is the exception pointing?

avatar image ExTheSea · Jun 04, 2013 at 05:52 PM 1
Share

I'm guessing that the vincentCamera is the camera in the corner. You're instantiating this gameobject wihtout setting position or rotation. This way it will be instantiated, i think, at (0,0,0).

You could try to add the Camera already to the prefab and then instead of instantiating it just root to it using transform.Find("PlayerCamera") or something like this.

Or you could instantiate the camera and also set the position and rotation.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by ExTheSea · Jun 02, 2013 at 01:56 PM

Not really like this you would do it more like this:

  var prefab : Transform;
 var script : SwitchCharacters; 
  
 var playerGO : GameObject; //<--Should be filled with the Gameobject the SwitchCharacters-Script is attached to.
  
 private var hasPlayed = false;
  
  
 function OnTriggerEnter () {
  
 var pos : Vector3;
  
 if (!hasPlayed){
  
 var newprefab = Instantiate (prefab);
  
 script = playerGO.GetComponent(SwitchCharacters);
 script.player02 = newprefab;
 
 
 hasPlayed = true;
  
 }
  
 }

Depending on where the instantiating script is attached to you can maybe replace the playerGO with something like transform, transform.parent, .... Whatever works in your case.

Comment
laurienash

People who like this

1 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by LostInCode404 · Jun 02, 2013 at 03:57 PM

I you want to switch between tps and fps. Create a static var fps and set its bool value to true. Now whatever you want to do in fps, do it in a if condition if fps is true and else if it is false do what you want to do in fps. If the trigger event happens, check tha value of var fps. If its true set it to false, else it is false, set it to true. Hope u understood. I will get u the script in a few days.

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ExTheSea · Jun 02, 2013 at 04:10 PM 0
Share

Please read the question first. The switching is working already but she has a problem when instantiating a new thirdpersoncontroller.

Also you should avoid using static variables if possible.

avatar image

Answer by DubstepDragon · Jun 05, 2013 at 04:32 PM

This is very easy. All you need is a 3rd person controller with the camera where the head is, so as to look first person, and just move the camera back however you want: If you want to move it instantly, so it switches right away; if you want to scroll out to zoom out. You can just set up some sort of line that the camera moves on.

Comment

People who like this

0 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bodec · Jun 06, 2013 at 01:34 PM 0
Share

I am currently working on this myself and have it jumping between two spots and am about to start working on a smooth transition between the two here is the code it works off the left mouse button but that can be changed with ease.

 using UnityEngine;
 using System.Collections;
 
 public class CameraControl : MonoBehaviour {
     public Transform myTransform;
     public Transform target;
     
     private float offsetY = 1.5f;
     private float offsetZ = -6.0f;
     public Vector3 maxDistance;
     public Vector3 minDistance;
     public Vector3 curDistance;
     public bool cameraSetAt;
     
     public float smoothingSpeed = 1.0f;
     private float smoothingVelocity;
     
     
     void Awake(){
         target = GameObject.FindWithTag("Player").GetComponent<Transform>();
         myTransform = transform;
     }
     // Use this for initialization
     void Start () {
         cameraSetAt = true;
         curDistance = new Vector3 (target.position.x, target.position.y + offsetY, target.position.z + offsetZ);
     }
     
     // Update is called once per frame
     void Update () {
         maxDistance = new Vector3 (target.position.x, target.position.y + offsetY, target.position.z + offsetZ);
         minDistance = new Vector3 (target.position.x, target.position.y, target.position.z);
         // Changes the cameras current position
         if(Input.GetButtonDown("Fire1")){
             if(cameraSetAt){
                 cameraSetAt = false;
                 Debug.Log("false");
             }else{
                 cameraSetAt = true;
                 Debug.Log("true");
             }    
         }
     }
     
     void LateUpdate(){
 
         if(cameraSetAt)
             myTransform.position = maxDistance;
         if(!cameraSetAt)
             myTransform.position = minDistance;
         
     }
     
 }
avatar image bodec · Jun 06, 2013 at 01:37 PM 0
Share

if need be move everything in awake to the start.

avatar image DubstepDragon · Jun 08, 2013 at 03:06 PM 0
Share

Your code is very interesting and peculiar... I had the impression that this works too:

 using UnityEngine;
 using System.Collections;
 
 public class ModifiedGravitronThingy : MonoBehaviour {
     
     Vector3 gravity;
     
     void Start() {
         gravity = Physics.gravity;
     }
     
     void Update() {
         Physics.gravity = gravity;
         
         if(Input.GetKeyDown(KeyCode.Q)) {
             gravity.x = 0;
             gravity.y = 2;
             gravity.z = 0;
         }
         
         if(Input.GetKeyDown(KeyCode.E)) {
             gravity.x = 0;
             gravity.y = 0;
             gravity.z = 2;
         }
     }
     
 }

However it stops after the RigidBody lies still... :/

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate and initialize script 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Instantiate new third person controller at the exact point first third person controller is destroyed 1 Answer

How to create a Graph in Unity 1 Answer

Boolean check not referenced correctly from other script 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges