Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by vProject · Feb 29, 2016 at 08:14 AM · cameratop down shooter

Camera between mouse and object. top down 2d. c#

Context

I am working on a top down 2d shooter. The player moves with WASD, and always faces the mouse.

I need a camera that stays at the mid point between the player and the cursor, unless that mid point is past a certain radius away from the player. The camera cant leave that radius.

Question

I am trying to c# code a camera that does 3 things:

(I've included an image below to better explain what I mean)

  • Stay directly at the mid point of the player object and the mouse. figure A

  • The Camera can not go past a certain distance / radius away from the player. (even if mid point between player and camera exceeds that distance) figure B

  • Keeps track of the distance between the player object and camera object. (so I can use that variable later)

alt text

I have exhausted my google-fu, and looked through so many script examples, and I keep coming up just short.

Any help would be greatly appreciated. Thankyou.

example-vision.jpg (150.5 kB)
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 Ali-hatem · Feb 29, 2016 at 10:56 AM 0
Share

have you done any coding i am working in unity now & i have done what you need but i don't know what do you mean by " faces the mouse cursor " & i don't know how to get the cursor position without click so i added a target.transform ins$$anonymous$$d to let you get the idea hope you or any one can replace the target.transform position with the cursor position here is the code :

using UnityEngine; using System.Collections;

 public class $$anonymous$$ov : $$anonymous$$onoBehaviour 
 {
 
     float midpointX;
     float midpointY;

 // if you place the target object in unity more than 6 camera will do nothing
     public float limit = 6 ;.
 
     public Camera cam;
 
     public Transform target;
 
     void Update () 
     {
         midpointX = transform.position.x + target.transform.position.x / 2;
         midpointY = transform.position.y + target.transform.position.y / 2;
         if (target.position.x <= limit & target.position.y <= limit) 
         {
             cam.transform.position = new Vector3 (midpointX,midpointY,-10);
         }
     }
 }
avatar image vProject Ali-hatem · Feb 29, 2016 at 05:20 PM 0
Share

Hi, thanks for your reply. I will take a look at the code later. I have got a fully working JS version, doing exactly what I need, I've posted it as an answer to my original question.

6 Replies

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

Answer by Adam_Benko · Dec 12, 2018 at 09:06 PM

Here is a c# version of the code: All credit goes to @MD_Reptile :)

 public class CameraControll : MonoBehaviour
 {
     public Transform Parent; // Whatever you want the camera locked to
     public Transform Obj; // The object to place the camera on
     public float Radius = 4.5f;
     float Dist;
     Vector3 MousePos1, MousePos2, ScreenMouse, MouseOffset;
     public void Update()
     {
         MousePos1 = Input.mousePosition;
         // the below line assumes this script is attached to a camera object
         ScreenMouse = GetComponent<Camera>().ScreenToWorldPoint(new Vector3(MousePos1.x, MousePos1.y, Obj.position.z - GetComponent<Camera>().transform.position.z));
         MouseOffset = ScreenMouse - Parent.position;
         MousePos2 = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -transform.position.z));
         //Obj.position.y = ((MousePos2.y - Parent.position.y) / 2.0) + Parent.position.y;
         //Obj.position.x = ((MousePos2.x - Parent.position.x) / 2.0) + Parent.position.x;
         Obj.position = new Vector3((MousePos2.x - Parent.position.x) / 2.0f + Parent.position.x, (MousePos2.y - Parent.position.y) / 2.0f + Parent.position.y, Obj.position.z);
 
         Dist = Vector2.Distance(new Vector2(Obj.position.x, Obj.position.y), new Vector2(Parent.position.x, Parent.position.y));
 
         if (Dist > Radius)
         {
             var norm = MouseOffset.normalized;
             //Obj.position.x = norm.x * Radius + Parent.position.x;
             //Obj.position.y = norm.y * Radius + Parent.position.y;
             Obj.position = new Vector3(norm.x * Radius + Parent.position.x, norm.y * Radius + Parent.position.y, Obj.position.z);
         }
     }
 }
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 vProject · Dec 13, 2018 at 10:55 PM 0
Share

$$anonymous$$arked as answered for visibility.

Credit to @$$anonymous$$D_Reptile (who's reply I can't mark as an answer)

avatar image SpuneRune · Oct 13, 2020 at 11:36 PM 0
Share

It works, but its all glitchy. It shakes a little when i move the character and glitches out when i move it around

avatar image Itharen · Jan 20 at 06:44 PM 0
Share

This was a nice start, but I've done some improvements....:

 public class CameraControlMain2D : MonoBehaviour {
   [SerializeField] private Transform playerTransform;
 
   private float radius = 30f;
   private float smoothTime = 0.3F;
   private Vector3 velocity = Vector3.zero;
 
   public void Update() {
     Vector3 mousePosition = MousePosition.GetMouseWorldPosition(); 
 
     Vector3 newPosition = new Vector3(
       (mousePosition.x - playerTransform.position.x) / 2f + playerTransform.position.x,
       (mousePosition.y - playerTransform.position.y) / 2f + playerTransform.position.y,
       transform.position.z
     );
 
     float dist = Vector2.Distance(
       new Vector2(newPosition.x, newPosition.y), 
       new Vector2(playerTransform.position.x, playerTransform.position.y)
     );
 
     if (dist > radius) {
       Vector3 mouseOffset = mousePosition - playerTransform.position;
       var norm = mouseOffset.normalized;
       newPosition = new Vector3(
         norm.x * radius + playerTransform.position.x, 
         norm.y * radius + playerTransform.position.y, 
         transform.position.z
       );
     }
     transform.position = Vector3.SmoothDamp(transform.position, newPosition, ref velocity, smoothTime);
   }
 }
 
 public class MousePosition {
   // Get Mouse Position in World with Z = 0f
   public static Vector3 GetMouseWorldPosition() {
     Vector3 vec = GetMouseWorldPositionWithZ(Input.mousePosition, Camera.main);
     vec.z = 0f;
     return vec;
   }
 
   public static Vector3 GetMouseWorldPositionWithZ(Vector3 screenPosition, Camera worldCamera) {
     Vector3 worldPosition = worldCamera.ScreenToWorldPoint(screenPosition);
     return worldPosition;
   }
 }
avatar image
1

Answer by vProject · Feb 29, 2016 at 05:22 PM

Posting this here for anyone looking for this sort of thing in the future.

This works exactly as shown in the pictures, It's JS though.

 #pragma strict
 
 var Parent : Transform; // Whatever you want to camera locked to
 var Obj : Transform; // The object to place the camera on
 var Radius = 4.5;
 var Dist : float;
 var MousePos1 : Vector3;
 var MousePos2 : Vector3;
 var ScreenMouse : Vector3;
 var MouseOffset : Vector3;
  
 function Update() {
     MousePos1 = Input.mousePosition;
     ScreenMouse = GetComponent.<Camera>().main.ScreenToWorldPoint(Vector3(MousePos1.x, MousePos1.y, Obj.position.z-GetComponent.<Camera>().main.transform.position.z));
     MouseOffset = ScreenMouse - Parent.position;
 
     MousePos2 = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, -transform.position.z));
     Obj.position.y = ((MousePos2.y - Parent.position.y)/2.0)+Parent.position.y;
     Obj.position.x = ((MousePos2.x - Parent.position.x)/2.0)+Parent.position.x;
      
     Dist = Vector2.Distance(Vector2(Obj.position.x, Obj.position.y), Vector2(Parent.position.x, Parent.position.y));
      
     if(Dist > Radius){
         var norm = MouseOffset.normalized;
         Obj.position.x = norm.x*Radius + Parent.position.x;
         Obj.position.y = norm.y*Radius + Parent.position.y;
     }
 }
Comment
Add comment · Show 2 · 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 RealErrorMessage · Apr 24, 2016 at 05:11 PM 0
Share

Is there any reason I can only find this script in JS? I'm trying to use C#, but everywhere I find something like this everyone uses JS.

avatar image MD_Reptile · Dec 10, 2018 at 10:20 PM 1
Share

UNTESTED C# CONVERSION:

 public class CamScript : $$anonymous$$onoBehaviour
 {
     Transform Parent; // Whatever you want to camera locked to
     Transform Obj; // The object to place the camera on
     float Radius = 4.5f;
     float Dist;
     Vector3 $$anonymous$$ousePos1, $$anonymous$$ousePos2, Screen$$anonymous$$ouse, $$anonymous$$ouseOffset;
     public void Update()
     {
         $$anonymous$$ousePos1 = Input.mousePosition;
         // the below line assumes this script is attached to a camera object
         Screen$$anonymous$$ouse = GetComponent<Camera>().ScreenToWorldPoint(new Vector3($$anonymous$$ousePos1.x, $$anonymous$$ousePos1.y, Obj.position.z - GetComponent<Camera>().transform.position.z));
         $$anonymous$$ouseOffset = Screen$$anonymous$$ouse - Parent.position;
         $$anonymous$$ousePos2 = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -transform.position.z));
         //Obj.position.y = (($$anonymous$$ousePos2.y - Parent.position.y) / 2.0) + Parent.position.y;
         //Obj.position.x = (($$anonymous$$ousePos2.x - Parent.position.x) / 2.0) + Parent.position.x;
         Obj.position = new Vector3(($$anonymous$$ousePos2.x - Parent.position.x) / 2.0f + Parent.position.x, ($$anonymous$$ousePos2.y - Parent.position.y) / 2.0f + Parent.position.y, Obj.position.z);
  
         Dist = Vector2.Distance(new Vector2(Obj.position.x, Obj.position.y), new Vector2(Parent.position.x, Parent.position.y));
  
         if (Dist > Radius)
         {
             var norm = $$anonymous$$ouseOffset.normalized;
             //Obj.position.x = norm.x * Radius + Parent.position.x;
             //Obj.position.y = norm.y * Radius + Parent.position.y;
             Obj.position = new Vector3(norm.x * Radius + Parent.position.x, norm.y * Radius + Parent.position.y, Obj.position.z);
         }
     }
 }
avatar image
1

Answer by charlessylvander · Oct 02, 2020 at 08:40 AM

@vProject check this out!

https://youtu.be/LFe017d-S58,@vProject I found a really helpful video on this. It is actually a lot simpler than you think!

https://youtu.be/LFe017d-S58

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 smsyaz1 · Jul 12, 2021 at 07:34 AM 0
Share

thank you so much! I've searched high and low but couldn't find the answer. I know this thread is old but I just had to say thank you

avatar image
0

Answer by boURiNEtte · Dec 27, 2016 at 08:10 AM

Hi I just began to port an old unfinished project on Flash to Unity a few days ago. It's 2D topdown too. I did find a way to make a similar kind of camera script (but simpler).

My Camera and my Player aren't parent nor child from each other. Works fine for the meantime.

Here's the script in UnityScript(js): followplayer.js attached to the camera.

 #pragma strict
 
 private var player:GameObject;
 private var mousePosition:Vector2;
 private var playerPosition:Vector2;
 
 function Start () {
     player = GameObject.FindWithTag("Player");
 }
 
 function Update ():void {
     mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     playerPosition = player.transform.position;
     
     SmoothFollow_Mouse(mousePosition, playerPosition);
 }
 
 function SmoothFollow_Mouse(a:Vector2, b:Vector2):void {
 var c:Vector2 = (a + b)/2;
 transform.position.x = (b.x + c.x)/2;
 transform.position.y = (b.y + c.y)/2;
 }
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
avatar image
0

Answer by osybear · Dec 13, 2017 at 02:57 PM

         Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
         Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
 
         float rayLength;
 
         if (groundPlane.Raycast(cameraRay, out rayLength))
         {
             Vector3 pointToLook = cameraRay.GetPoint(rayLength);
             Vector3 NewPos = (gameObject.transform.position + pointToLook) / 2;
             NewPos = new Vector3(NewPos.x, 10f, NewPos.z - 3.5f);
             playerCam.transform.position = NewPos;
         }

My camera is angled by 70* in the x and it works as expected.

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
  • 1
  • 2
  • ›

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

Need help with camera in top down shooter 0 Answers

Cinemachine 2D LookAt 0 Answers

Rotate camera and player with mouse 2d top-down shooter in C# 1 Answer

Top Down Camera View Problem 1 Answer

How to add a frame to my camera view in the game view? 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