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
3
Question by Kmulla · Apr 18, 2012 at 07:09 PM · c#transformvector3translate

Move GameObject along a line according to the players look direction

I have two GameObjects: The Main Camera, which the player can rotate, and a box, which only moves in the z-axis. I need the box to always be in the Main Camera's forward position, and newer move away from it's line.

I've tried to illustrate it: alt text

Additional information: I'm coding in C# and have tried different solutions with vectors, but just can't get my head around a solution.

movego.png (6.3 kB)
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

3 Replies

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

Answer by Bunny83 · Apr 19, 2012 at 11:44 AM

You can also use the Plane object so you have an infinite large "collider" plane.

A mathematical plane is defined by it's normal vector and the distance from 0,0,0. You can create a plane be hand in the normal vector to define in which direction the plane should face and any point in world coordinates that is on the plane:

 private Plane m_Plane;
 
 void Start()
 {
     // This plane will be at 0,0,20 and face towards the origin(0,0,0)
     m_Plane = new Plane(-Vector3.forward, new Vector3(0,0,20));
 }
 
 //[...]
 Ray ray = Camera.main.ViewportPointToRay (new Vector3(0.5,0.5,0));
 float dist;
 if (m_Plane.Raycast(ray, out dist))
 {
     Vector3 hitPoint = ray.GetPoint(dist);
     // use it
 }

The plane object isn't a visual component. It's just the mathematical representation of a plane.

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 Kmulla · Apr 19, 2012 at 01:30 PM 0
Share

Thank you so much! This works exactly as intended, and it's so simple!

avatar image
2

Answer by TheDarkVoid · Apr 18, 2012 at 08:33 PM

you can use a ray cast hitting an invisible collier which will act as your line and place the gameObject at the raycast contact point.

see: http://unity3d.com/support/documentation/ScriptReference/RaycastHit-point.html and: http://unity3d.com/support/documentation/ScriptReference/RaycastHit.html

for help

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 Kmulla · Apr 19, 2012 at 09:54 AM 0
Share

This is the solution! Since there's a place where the line isn't straight, this would work great there too. I wasn't even aware that the Raycast had a point-function. Thank you :) … And it's a great excuse for finally learning how to use the layer masks!

avatar image
1

Answer by Tseng · Apr 18, 2012 at 08:54 PM

Just use the camera transforms forward direction and zero out the y axis. that's important because the camera always "looks down" on the player, but you don't want to move in that direction (cause you'd go through the ground :P)

 Vector3 forward = Camera.main.transform.foward;
 forward.y = 0f;    
 // Move the box    
 transform.Translate(forward * speed * Time.deltaTime);
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 Kmulla · Apr 19, 2012 at 10:00 AM 0
Share

This solution needs a few adjustments to work, like locking the x-axis as well :) When I tried it out, the box kept moving in the direction the player was looking, and I couldn't think of a quick solution to make it stop at the wanted place, but I'm sure it would work with additional coding! … nevertheless kingk614's solution is the answer to this problem AND another I wasn't looking forward to. But thanks anyway :)

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

transform.right is using Global Space rather than local 1 Answer

Need some help with to grap .x with a Raycasthit (c#) 1 Answer

Transform.Position Collision Issue 0 Answers

C# 2d Spawn Randomly along Camera Borders 1 Answer

UnityEngine.Component:get_transform() Error 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