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 SimonDenton · Apr 21, 2012 at 11:38 AM · rotationraycasttransform

How to place an object at the centre of another and move it along.

For a skating game I am doing raycasting to detect the player being ontop of a mesh with the tag "Metal Rail". The code I am currently writing needs to: Position the player at the middle of the rail In the direction the player is moving, push them along the rail at the current moving speed multiplied by another factor.

So skateSpeed is the speed we normally moving and grindSpeed is that extra boost of speed we get. What I am having trouble with is moving the player along and placing the player in the middle of the rail so we're not grinding far off the mesh. Here is my attempt in C#:

 //Grinding
         
           Vector3 down = transform.TransformDirection(Vector3.down);
          if (Physics.Raycast(transform.position, down, out hit))
         {
             if (hit.collider.CompareTag("MetalRail"))
             {
                 print("We're grinding!!!");
             transform.Translate(0,Vector3.forward * Time.deltaTime * skateSpeed * grindSpeed,0);
                 transform.rotation = collider.GetComponent.rotation;
             Grind(); //Animation cues
             }
             
             
         }

Please assist me in correctly performing the translation to move the player along and position the player at the centre of the rail. Many thanks!

Comment
Add comment · Show 5
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 Flash · Apr 21, 2012 at 12:17 PM 1
Share

Have you tried compensating for the "slipping of the rail" by simply setting the x or z of you player to the x or z of the rail?

avatar image SimonDenton · Apr 21, 2012 at 12:23 PM 0
Share

Thanks for the prompt comment! I tried to in the transform.rotation = collider.GetComponent.rotation; but I don't think that part is quite right. I replaced the translation with transform.forward = Vector3.Normalize(new Vector3(0f, 0f, grindSpeed * skateSpeed)); and it makes the player stay still but it might be on the right track in terms of the grind movement.

avatar image Flash · Apr 21, 2012 at 12:58 PM 1
Share

Let's say your player's local x is the axis pointing forward. Couldn't you just move him along his z axis to center him once he's on the rail.

Vector3 newZ = new Vector3(transform.position.x, transform.position.y, hit.collider.transform.position.z); transform.position = newZ;

I'm not sure that would work in your case but I think it's worth a try.

avatar image SimonDenton · Apr 21, 2012 at 01:15 PM 0
Share

I did:

if (hit.collider.CompareTag("$$anonymous$$etalRail")) { Vector3 newZ = new Vector3(transform.position.x, transform.position.y, hit.collider.transform.position.z); transform.position = newZ;

             print("We're grinding!!!");

}

And it actually snaps the player to the middle of the current rail! Thanks! Positioning is well done!

avatar image Flash · Apr 22, 2012 at 01:06 AM 1
Share

So it works? I'll post the solution as an answer below then. Would you $$anonymous$$d selecting it as the correct answer so that we can close this question? Thanks.

1 Reply

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

Answer by Flash · Apr 22, 2012 at 01:05 AM

Let's say your player's local x is the axis pointing forward. Couldn't you just move him along his z axis to center him once he's on the rail.

 if (hit.collider.CompareTag("MetalRail")) {
    Vector3 newZ = new Vector3(transform.position.x, transform.position.y, hit.collider.transform.position.z);
    transform.position = newZ;
 }
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 SimonDenton · Apr 22, 2012 at 02:30 AM 0
Share

This is partially an answer. The second part is actually pushing me forward. Your code makes the player positioned in the middle of the rail, which is what I asked for. But how do we push the player along?

I'll give you the credit of answering the question anyhow.

avatar image Flash · Apr 22, 2012 at 02:37 AM 1
Share

Well if we presume that the local x of you player is the forward direction you could simply go:

transform.position.x += SlideSpeed * Time.deltaTime;

When you've detected that the player is on the rail.

avatar image SimonDenton · Apr 22, 2012 at 02:58 AM 0
Share

Oh man we're so close! The only thing to tackle now is storing in a temporary variable. I did:

Vector3 sliding = transform.position.x; transform.position.x += grindSpeed * Time.deltaTime;

             transform.position = sliding;

but with no success. But hey, this error asside and we're done! Another thumbs up for you :)

avatar image Flash · Apr 22, 2012 at 05:12 PM 0
Share

In the code you just posted you declare a Vector3 variable called "sliding" and assign the x position of your objects transform (transform.position.x).. Well you see the x property of the transform.position is not a Vector3 but a float. What your code is doing is storing the x property of the position as a Vector3 (Which should be a float). You then move your object on the x axis (transform.position.x += grindSpeed * Time.deltaTime). Then you're "resetting" the position of your object (transform.position = sliding) which is not allowed since transform.position needs a Vector3 not a float (The variable "sliding" is a float in this case).

I'm not sure exactly what you want to do here so if you could explain a bit further that would be great.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Making an object follow the mouse in 3D world 0 Answers

Object slope rotation issue 0 Answers

Rotate raycast origin with gameobject 0 Answers

Unity Rotate Raycast on Quaternion 1 Answer

Cant understand why object rotates when setting transform.up to normal 2 Answers


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