Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by Ozzyel-PT · Sep 09, 2013 at 06:45 AM · cameraswitchclone

Switch camera to cloned object.

Hello again.

I have a problem with my camera, it follows a player like a 3rd person camera, it is fine but when my player dies an instantiated ragdoll is cloned and player object is destroyed. So I want my camera follows that ragdoll. I got this so far:

 #pragma strict
 var target : Transform;
 var height : float;
 var distance : float;
 var shoulderView : float;
 
 private var targetPosition : Vector3;
     
 function Update(){
     if(_playerHealth.playerDead){ 
         var sf : _cameraScript = Camera.main.GetComponent(_cameraScript);
         sf.target = transform.Find("Ragdoll(Clone)");
     }    
 }
 
 function LateUpdate () {
 
     if(!_playerHealth.playerDead){
         transform.LookAt(aimTarget);
     }else{
         transform.LookAt(target);
     }
     
     targetPosition = target.position + target.right * shoulderView + target.up * height - target.forward * distance;
     transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * 30.0);
 
 }

I've tried things like replacing target with another variable finding the clone with tag etc.. And I have the same error: NullReferenceException at targetPosition variable in LateUpdate.

Thanks in advance.

Comment
Add comment · Show 2
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 Ozzyel-PT · Sep 09, 2013 at 06:04 PM 0
Share

Thanks, I get the cloned ragdoll on target in my camera, but now I can't get the ragdoll position, even if I put the ragdoll directly in the scene its coordinates don't change. Any idea? any way here's the script I used:

function Update () {

 if(_playerHealth.playerDead && !swap){ 
     target =  GameObject.FindWithTag("playerDead").transform;
     swap = true;
 }    
 
     targetPosition = target.position + target.right * shoulderView + target.up * height - target.forward * distance;
     transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * 30.0);
     transform.LookAt(target);

 if (!target) return;

}

avatar image Ozzyel-PT · Sep 09, 2013 at 11:14 PM 1
Share

Ok, I got it working, about the ragdoll I had to get the ragdoll hips or root position , some like:

 targetPosition = target.Find("Root/Hip").position + Vector3 (3.0 , 3.0 , 3.0);
 transform.LookAt(target.Find("Root/Hip").position);

Just added that to my player dead condition and the camera can follow the ragdoll well. Thanks for helping.

Fattie, can you answer? so I can mark it as answered.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Fattie · Sep 09, 2013 at 07:59 AM

Just FYI. you definitely shouldn't be doing this sort of thing in Update. Don't forget, update happens 100s of times every second. if you think about it, it doesn't make sense to check "is player dead" 100s of times a second. you need to swap the camera AT THE TIME the gameobject is destroyed, when it happens.

further, really you just shouldn't use LateUpdate, it's very unnecessary and almost certainly mistaken here. hope it helps a bit!

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

How to set main camera? 3 Answers

setting a character controller orientation by script 1 Answer

Switching camera effects 1 Answer

Switching Several Cameras(HorrorGame) 1 Answer

Swapping Cameras Disables Keyboard Input 3 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