• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by IKilledKenny_2 · Apr 07, 2015 at 12:42 AM · cameracollisiongameobjectcollider

Smooth Camera

Hello Unity3D i have a question about camera movement?How can i make it that when my character collides with with another character the camera moves to a 2d view and when there is no collision being dealt to the other character the camera moves back to the original spot?For example i want the camera to move from point (A) w$$anonymous$$ch is t$$anonymous$$rd person view and when my character collides with the other character it goes to point (B) 2d view.If anyone knows how i can do t$$anonymous$$s?Can you please tell me how?

P.S t$$anonymous$$s is what im aiming for link text

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

2 Replies

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

Answer by _Gkxd · Apr 07, 2015 at 03:18 PM

There are two sets of information that you want to keep track of: the 3rd person camera view, and the "2d" camera view. To switch between these two transforms smoothly, you can use Vector3.Lerp to interpolate the position of the camera and Quaternion.Lerp to interpolate the rotation of the camera.

Let's refer to the camera position/rotations as camPos3d, camRot3d, camPos2d, camRot2d. Inside your LateUpdate, you would do somet$$anonymous$$ng along the lines of t$$anonymous$$s (pseudocode):

 camera.transform.position = Vector3.Lerp(camPos3d, camPos2d, t);
 camera.transform.rotation = Quaternion.Lerp(camRot3d, camRot2d, t);

Here t is a float value between 0 and 1. If t is 0, the camera position will be in the 3rd person view. If t is 1, the camera will be in 2d view. If t is somewhere in between, the camera will be in a position somewhere in between.

What you can do now is in your collision handling code, if you're colliding with the right t$$anonymous$$ng, increase t gradually. If you're not colliding with the right t$$anonymous$$ng, decrease t gradually, clamping at 0/1.

Basic Example Code (C#)

 using UnityEngine;
 using System.Collections;
 
 public class SmoothCameraSwapExample : MonoBehaviour {
 
     private GameObject mainCamera;
     private GameObject dummyCameraA;
     private GameObject dummyCameraB;
 
     private float cameraLerpAmount;
 
     void Start () {
         mainCamera = GameObject.FindGameObjectWithTag ("MainCamera");
 
         // The dummy cameras are empty game objects used to keep track of the camera positions
         dummyCameraA = new GameObject ("Dummy Camera A");
         dummyCameraB = new GameObject ("Dummy Camera B");
 
         dummyCameraA.transform.SetParent (transform);
         dummyCameraB.transform.SetParent (transform);
 
         dummyCameraA.transform.localPosition = new Vector3 (10, 5, 0);
         dummyCameraB.transform.localPosition = new Vector3 (4, 0, -4);
 
         dummyCameraA.transform.LookAt (transform);
         dummyCameraB.transform.LookAt (transform);
     }
 
     void Update () {
         // Space switches between the two camera positions
         if (Input.GetKey (KeyCode.Space)) {
             cameraLerpAmount = Mathf.Clamp01 (cameraLerpAmount + 0.1f);
         } else {
             cameraLerpAmount = Mathf.Clamp01 (cameraLerpAmount - 0.1f);
         }
     }
 
     void LateUpdate() {
         mainCamera.transform.position = Vector3.Lerp (dummyCameraA.transform.position, dummyCameraB.transform.position, cameraLerpAmount);
         mainCamera.transform.rotation = Quaternion.Lerp (dummyCameraA.transform.rotation, dummyCameraB.transform.rotation, cameraLerpAmount);
     }
 }
 

T$$anonymous$$s should be the basics of what you want. Just attach it to a GameObject and press space to swap views. The positions are currently hard-coded, but I t$$anonymous$$nk you'll get the idea. (Also, you need a camera tagged "MainCamera" in your scene.) Feel free to ask for more clarification.

Comment
Add comment · 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 IKilledKenny_2 · Apr 10, 2015 at 01:03 AM 0
Share

Thank You So Much =D!But i have one more question?how would this script work if i was punching an object and if i don't click anything for over 1 second the camera goes back to the starting position?

avatar image _Gkxd · Apr 10, 2015 at 02:42 AM 0
Share

This is just an example on how you would interpolate two camera transforms. The way that the lerp amount changes inside Update() is completely up to you, so you would probably want to experiment with things other than what I put in Update().

avatar image IKilledKenny_2 · Apr 10, 2015 at 05:18 PM 0
Share

Oh..=o Ok You have no idea how much i needed this.Thank You Again ^.^

avatar image
0
Wiki

Answer by IKilledKenny_2 · Apr 07, 2015 at 10:05 PM

Do You mean like t$$anonymous$$s?

 var player : GameObject;
 var gravity : float = 60.0;
 var myTransform : Transform;
 var speed:  float =5;
 var mass: float = 3.0; // the lower the mass, the $$anonymous$$gher the impact
 var $$anonymous$$tForce: float = 2.5; // impact "force" when $$anonymous$$t by rigidbody 
 var mainCamera : Camera;
 var target     : Transform;
 var smooth     : float = 5;
 var normtarget : Transform;
 var moveSpeed : float = 5;
 var camPos3d  : GameObject;
 var camPos2d  : GameObject;
 var t         : float = 5;
 var camRot3d  : float = 50;
 var camRot2d  : float = 50;
 
 private var impact = Vector3.zero; // character momentum 
 private var character: CharacterController;
  
 function Awake() {
         myTransform = transform;
 }
 function Start() {
                  // Keep a note of the time the movement started.
         enemy = GameObject.FindWithTag("Dummy").transform;
         character = GetComponent(CharacterController);
 }    
 
 
     
 
  function AddImpact(force: Vector3){
      var dir = force.normalized;
      dir.z = 0.5; // add some velocity upwards - it's cooler t$$anonymous$$s way
      impact += dir.normalized * force.magnitude / mass;
  }
 
 
 
 function Update(){
      if (impact.magnitude > 0.2){ // if momentum > 0.2...
          character.Move(impact * Time.deltaTime); // move character
      }
      // impact vanishes to zero over time
      impact = Vector3.Lerp(impact, Vector3.zero, 5*Time.deltaTime);
 }
 
 
 
 
 function OnCollisionEnter(collision : Collision){
  
  if(collision.gameObject.tag == "Dummy"&&player.animation.IsPlaying("Sidekick")){
 enemy.transform.gameObject.animation.Play("Hit1");
 AddImpact(enemy.relativeVelocity * $$anonymous$$tForce);
 }
  if(collision.gameObject.tag == "Dummy"&&player.animation.IsPlaying("Sidekick_lightning")){
 collision.transform.gameObject.animation.Play("Hit1");
 mainCamera.transform.position = target.transform.position - target.transform.forward * smooth * moveSpeed;
 mainCamera.transform.Rotate(0,-180,90);
 camera.transform.position = Vector3.Lerp(camPos3d, camPos2d, t);
 camera.transform.rotation = Quaternion.Lerp(camRot3d, camRot2d, t);
 AddImpact(enemy.relativeVelocity * $$anonymous$$tForce);
 }
   if(collision.gameObject.tag == "Dummy"&&player.animation.IsPlaying("Sailor_kick")){
 enemy.transform.gameObject.animation.Play("Hit1_Reverse");
 AddImpact(enemy.relativeVelocity * $$anonymous$$tForce);
 }
   if(collision.gameObject.tag == "Dummy"&&player.animation.IsPlaying("Ninja_Kicks")){
 enemy.transform.gameObject.animation.Play("Hit1");
 mainCamera.transform.position = normtarget.transform.position - normtarget.transform.forward;
 AddImpact(enemy.relativeVelocity * $$anonymous$$tForce);
 }
   if(collision.gameObject.tag == "Dummy"&&player.animation.IsPlaying("Jab")){
 enemy.transform.gameObject.animation.Play("Hit1");
 enemy.transform.gameObject.rigidbody.AddForce(transform.forward * speed);
 }
   if(collision.gameObject.tag == "Dummy"&&player.animation.IsPlaying("Cross")){
 enemy.transform.gameObject.animation.Play("Hit1_Reverse");
 }
   if(collision.gameObject.tag == "Dummy"&&player.animation.IsPlaying("Hook")){
 enemy.transform.gameObject.animation.Play("Hit1");
 }
  if(collision.gameObject.tag == "Dummy"&&player.animation.IsPlaying("Punch4")){
 enemy.transform.gameObject.animation.Play("Hit_Stomach");
 
          
     }
  }
 


Sorry i pasted the whole code i didnt want to confuse you by leaving important stuff out.

Comment
Add comment · 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 _Gkxd · Apr 07, 2015 at 11:28 PM 0
Share

I've added a working example to my answer above. I think you'll be able to figure the rest out.

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

19 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 avatar image

Related Questions

How to stop Camera from going into colliders 3 Answers

How can i check if GO collided with collider starting with name (string) 1 Answer

Camera Collison with scene objects 1 Answer

Help!Crash! FatalError"Callback registration failed kMaxCallback" 1 Answer

Gameobject collision with Terrain C# 4 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