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
0
Question by StatecLata · Oct 30, 2014 at 04:10 AM · camerateleport

Camera Following character that teleported

I've already created a "smooth" camera script to follow the character, the thing is i've added a teleport spot that teleports the character to another place (x,y) but the camera also "teleports" with him i want the camera to follow up like showing the road to him is it possible?

It's a 2D game btw, thanks!

Script:

using UnityEngine; using System.Collections;

public class CameraController : MonoBehaviour {

 public Transform Player;

 public Vector2
     Margin,
     Smoothing;

 public BoxCollider2D Bounds;

 private Vector3
 _min,_max;

 public bool isFollowing { get; set; }

     public void Start()
 {
     _min = Bounds.bounds.min;
     _max = Bounds.bounds.max;
     isFollowing = true;
 }

 public void Update()
 {
             var x = transform.position.x;
             var y = transform.position.y;

             if (isFollowing) {
                     if (Mathf.Abs(x - Player.position.x) > Margin.x)
                             x = Mathf.Lerp(x, Player.position.x, Smoothing.x * Time.deltaTime);

                     if (Mathf.Abs (y - Player.position.y) > Margin.y)
                             y = Mathf.Lerp(y, Player.position.y, Smoothing.y * Time.deltaTime);

             }

             var cameraHalfwidth = camera.orthographicSize * ((float)Screen.width / Screen.height);
             x = Mathf.Clamp(x, _min.x + cameraHalfwidth, _max.x - cameraHalfwidth);
             y = Mathf.Clamp(y, _min.y + camera.orthographicSize, _max.y - camera.orthographicSize);

             transform.position = new Vector3 (x, y, transform.position.z);

     }

}

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

1 Reply

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

Answer by yashpal · Oct 30, 2014 at 08:50 AM

Hello @StatecLata

You can directly use script which is provided by unity named "SmoothFollow". You will find this script by following "Assets" tab -> "Import Package" -> "Scripts". And Add "SmoothFollow.js" this script attach to camera and give player as target.

Hope this will help you.

Comment
Add comment · Show 9 · 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 StatecLata · Oct 30, 2014 at 03:46 PM 0
Share

Thanks for your tip @yashpal although you're not helping me with the basic issue

avatar image yashpal · Oct 31, 2014 at 04:59 AM 0
Share

@StatecLata, you are using clamp in your script so it is always clamp value of your camera movement. so your character move inside your camera view than camera is follow player.

If you don't use this code then camera follow your player

  var cameraHalfwidth = camera.orthographicSize * ((float)Screen.width / Screen.height);
              x = $$anonymous$$athf.Clamp(x, _$$anonymous$$.x + cameraHalfwidth, _max.x - cameraHalfwidth);
              y = $$anonymous$$athf.Clamp(y, _$$anonymous$$.y + camera.orthographicSize, _max.y - camera.orthographicSize);



or you can put condition means

 if(!PlayerTranstortedbool) // Default PlayerTranstortedbool = false
 {
 var cameraHalfwidth = camera.orthographicSize * ((float)Screen.width / Screen.height);
                  x = $$anonymous$$athf.Clamp(x, _$$anonymous$$.x + cameraHalfwidth, _max.x - cameraHalfwidth);
                  y = $$anonymous$$athf.Clamp(y, _$$anonymous$$.y + camera.orthographicSize, _max.y - camera.orthographicSize);
 }

when player transported set PlayerTranstortedboolflag true

Hope this might help.

avatar image StatecLata · Oct 31, 2014 at 12:55 PM 0
Share

@yashpal Didn't see any difference to be honest

avatar image yashpal · Nov 02, 2014 at 08:48 AM 0
Share

@StatecLata, I use your code edit it and made one demo scene. it works fine when player teleport.

Steps to implement are following

1) download package from this link

2) import in new project.

3) open and run Scene "CameraFollow"

4) click any where in game screen to teleport player (RedCube).

Hope it is what you are looking for.

avatar image StatecLata · Nov 02, 2014 at 01:30 PM 0
Share

@yashpal i don't want the camera to follow the player into teleport, i want the camera to walk to the player mate!

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Camera Transform.Position (teleport) 2 Answers

How can I make a character disappear from view from one side to the other? 1 Answer

How can I program a 'wrap-screen'? 2 Answers

How to make camera position relative to a specific target. 1 Answer

Issues with player prefabs when I try to teleport it 1 Answer


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