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 Farenhiet · Jul 10, 2013 at 06:08 AM · cameradistancecursorlocation2.5d

Need to Find Distance Between Mouse Cursor and Object in a 2.5D SideScroller

I'm totally new to Unity though I've been coding a bit and I've made some stuff in GameMaker. Anyways I've been working on the basic stuff for a 2.5D sidescroller and I'm having trouble with refining the camera. The cursor will move independent of the camera, but WILL extend the camera somewhat subtly if pushed near the edge. I'm also trying to get the camera to extend to the left or the right of the character depending on which direction the character is facing. I've got basic camera movement and camera boundaries relative to the character. I just need help with subtle camera movements and set ups based on the cursor in relation to the character (i.e. a cursor to the right of a character means he's facing that direction and would have the camera extend that direction). I'm having trouble properly measuring the distance between the cursor and the character.

To my understanding, "Raycasts" might be the best way to figure this out, but after messing with them a while I'm quite lost on them. I'm not really good with coding and I'm used to C++ so I've been using C#. It'd be awesome for anyone to get back to me!

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
1
Best Answer

Answer by robertbu · Jul 10, 2013 at 06:15 AM

The simplest method of making the conversion is to use Camera.ScreenToWorldPoint(). As a starting point, you will need to provide or calculate the distance between the camera and the plane you are using for your 2D movements. The code would then be something like this (assumes the camera is looking towards positive 'Z'):

 Vector3 v3Pos = new Vector3(Input.mousePosition.x Input.mousePosition.y, distance);
 v3Pos = Camera.main.ScreenToWorldPoint(v3Pos);
 
 v3Pos = v3Pos - character.position;
 
 if (v3Pos.x > 0.0f) 
     Debug.Log("Cursor is in front of the character");
 else
     Debug.Log("Cursor is behind the character");
Comment
Add comment · Show 4 · 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 Farenhiet · Jul 10, 2013 at 08:49 PM 0
Share

Thanks! This got me a little farther, but I still can't manage to get it working.

 using UnityEngine;
 using System.Collections;
 
 public class SideScroller$$anonymous$$ouse : $$anonymous$$onoBehaviour {
     
     //$$anonymous$$ouse sensitivity
     public float sensitivity = 1.0F;
     
     //Default camera resting offsets, should be about two-thirds of the hard bounds
     public float xRest = 1.4F;
     public float yRest = 0.6F;
     
     void Update () {
             
         Vector3 camTemp = Camera.mainCamera.transform.position;
         Vector3 transTemp = GameObject.Find("construction_worker").transform.position;
         Vector2 cursTemp = new Vector2(Input.GetAxis("$$anonymous$$ouse X"), Input.GetAxis("$$anonymous$$ouse Y"));
         
         Vector3 cursor = new Vector3(Input.mousePosition.x, Input.mousePosition.y,0.0F);
         cursor = Camera.main.ScreenToWorldPoint(cursor);
         
         if (cursor.x >= transTemp.x)
         {
             if (cursor.x < transTemp.x + xRest)
             camTemp.x = transTemp.x + xRest;
             else if (cursor.x > transTemp.x + xRest)
             camTemp.x -= cursTemp.x * sensitivity;
             
         }
         else if (cursor.x < transTemp.x)
         {
             if (cursor.x > transTemp.x - xRest)
             camTemp.x = transTemp.x - xRest;
             else if (cursor.x < transTemp.x - xRest)
             camTemp.x -= cursTemp.x * sensitivity;
         }
         
         camTemp.y += cursTemp.y * sensitivity;
         
         Camera.mainCamera.transform.position = camTemp;    
     }
 }
 

There's my terrible code if you want to see. The camera does lock into position on either side of the character, but X mouse movement always moves the camera (rather than just when those two "else if" statements trigger) resulting in terrible jitter when I move the cursor towards the character. The camera also seems to switch sides only when I quickly move the mouse, rather than when I actually move it over the character.

I did try by subtracting the mouse world position by the characters transform as well:

 cursor = cursor - transTemp;

And I edited all the if statements to match appropriately, but had no luck. Any further help would be awesome, and again thanks for the response.

avatar image Farenhiet · Jul 10, 2013 at 09:02 PM 0
Share

Ohp. Sorry, I forgot to get the distance from the 2D plane to the camera. The plane is at 0 so I just got the Camera's Z position.

         Vector3 cursor = new Vector3(Input.mousePosition.x, Input.mousePosition.y,camTemp.z);

The camera doesn't constantly move with X mouse movement, so the boundaries seem to work, but the camera still won't switch sides unless I move the mouse X axis quickly (rather than position the cursor on the other side of the character).

avatar image robertbu · Jul 10, 2013 at 09:46 PM 0
Share

I take a look when I have more time this evening. If you are using the default setup with the camera looking towards positive 'z', then your distance will be '-camTemp.z'.

avatar image Farenhiet · Jul 11, 2013 at 01:24 PM 0
Share

Well I'll continue working on this code, I'm definitely getting close, and you did answer my question and helped me out a lot. So thanks again!

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

15 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

Related Questions

Camera distance from mesh not center 1 Answer

Check if position is on screen 0 Answers

Locking cursor, while moving camera. 0 Answers

Screen.lockCursor messes my rotation 1 Answer

Large scales, increased clipping planes, and performance 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