Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 overbound · Jun 08, 2017 at 06:24 AM · vector3floatcasting

Direction and Distance from One Object to Another

In the Unity manual it lists a method for getting the direction between two points

 // Gets a vector that points from the player's position to the target's.
 var heading = target.position - player.position;
 var distance = heading.magnitude;
 var direction = heading / distance; // This is now the normalized direction.

It looks like the example was done in javascript but I am using C# so I took that calculation and tried to apply it to my work:

 // calculate slant angle
 Vector3 originVector = new Vector3 (x1,y1,z);
 Vector3 outerVector = new Vector3 (x2,y2,z);
 Vector3 heading = originVector - outerVector;
 float distance = heading.magnitude;
 
 return heading/distance;

The problem I'm having is since magnitude is a float value and everything else is a Vector3 the types are not compatible. The example keeps the types generic but that doesn't seem right as they are specifically talking about making calculations with Vectors.

So my question is how to I get this to work in C# as intended in the linked manual page. Am I just supposed to cast?

Comment
Add comment · Show 1
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 bobisgod234 · Jun 08, 2017 at 06:37 AM 0
Share

This works fine for me, Vector3 has an operator for dividing by floats. Specifically what error messages are you getting, and what line does Unity say they are on?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kritoa · Jun 08, 2017 at 06:32 AM

What is your function supposed to return?

Vector3 / float is defined: it returns a Vector3 (it divides each component, x,y,z, by the second argument). So this should work just fine. What error are you getting?

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot modify a value type return value of `UnityEngine.Transform.position'. Can anyone help ? 1 Answer

Using a variable as a components for Vector3 1 Answer

Getting an array of floats from a .bytes asset 0 Answers

How to cut numbers after decimal point in coordinates? 1 Answer

I want to move a rigid object with following command 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