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 skyechuu · May 31, 2013 at 10:20 AM · cameraraycastcursor

Raycast length or camera to between mousePosition and character

Hello guys, i have simple question. in my game project i will use camera script that positioning between cursor and character. can i use length/2 of my raycast or what can i use? can you answer me in simply? thanks a lot guys... (the game is 2d perspective and processing in z=0 so character, cursor and bullets , all moving on just x and y axes.)

(EDIT1: in my platform game, my cahracter will cast a Fireball spell to direction of a cursor,mouse position. also my camera will focus to show between character and cursor. I mean smiliar to Soldat patform shooter game's camera style.)

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 PAHeartBeat · May 31, 2013 at 10:55 AM 0
Share

Hi

your answer is little-bit confusing. I can't understand what you want to position and for what purpose. I asume you use ray-cast for finds traget where you click and move bullet in which direction.

avatar image robertbu · Jun 01, 2013 at 02:36 PM 0
Share

$$anonymous$$ouse lives in 2D space (screen), but your game objects live in 3D space. This is no distance between the mouse position and an object position. You can calculate the distance between the camera position and the mouse.

1 Reply

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

Answer by Scribe · Jun 01, 2013 at 03:56 PM

If I understand the question then you can do this quite simply using:

 var MousePos : Vector3;
 var CharObject : Transform;
 
 function Update () {
     MousePos = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, -transform.position.z));
     transform.position.x = ((MousePos.x - CharObject.position.x)/2.0)+CharObject.position.x;
     transform.position.y = ((MousePos.y - CharObject.position.y)/2.0)+CharObject.position.y;
 }

What I understood was that you want the camera to be centred between your character and your mouse position. This will work if you set 'CharObject' to your character model, though I have not included any clamping of the mouse position so this will work best in fullscreen at the moment, though clamping would be easy to add.

Hope that helps,

Scribe

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 Scribe · Jun 01, 2013 at 03:57 PM 0
Share

Forgot to mention to attach this to your Camera

avatar image skyechuu · Jun 02, 2013 at 02:30 PM 0
Share

Thanks a lot, this is what I want. I learn that i can chose axis for distance select ($$anonymous$$ousePos*.y*) also thanks for that. I will attach what its looks like. but first i will edit a little bit.

avatar image skyechuu · Jun 05, 2013 at 11:23 PM 0
Share

Scribe's Code in Game. Thanks a Lot Scribe. http://www.youtube.com/watch?v=dlaZqEu1WEo

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

Logic to detect objects entering between the player and camera? 1 Answer

Raycasting with the origin of the ray coming from the cursor 2 Answers

Raycast not rotating with object it casts from 0 Answers

Camera Point-and-click Movement 2 Answers

Problem with Raycast hit 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