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 knuckles209cp · May 17, 2019 at 02:33 AM · javascriptraycasttransform

Move to closest point in front of player with raycast

I have my player, When he lands on the rail I turn on isKinematic and lerp his transform.position to the next point. Now what I would like to do, Is make it so he moves towards the closest point in front of him or the direction he is going. I thought this would be easy, but im finding this extremely difficult.

This is what I've thought of so far :

  1. create a raycast in front of the player

  2. point the raycast to the closest point with tag "point"

  3. move player towards raycast direction

This is what I have so far

 if( OnRail == true ){
     gameObject.GetComponent(Rigidbody).isKinematic = true;
     transform.position = Vector3.MoveTowards(transform.position, nearRailPoint.transform.position,Time.deltaTime * force / 3);
 }

I did try to come up with raycast code but nothings working. I am using javascript ( Yes i know, Im awful at learning code ), any help is appreciated thank you!

Comment
Add comment · Show 4
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 MadDevil · May 17, 2019 at 07:22 AM 0
Share

Your question is bit confusing.

Are you facing problem with the raycast code or the moveTowards code? How do you find nearRailPoint ?

are the "Point" located in front of the player ?

avatar image knuckles209cp MadDevil · May 17, 2019 at 10:26 AM 0
Share

Basically,When my player jumps onto the rail it will move along the rail in the direction its facing, I am trying to do this by getting the closest way point on the rail and setting the players transform.position to lerp to that waypoint on the rail. Does that make sense?

avatar image DCordoba · May 17, 2019 at 09:00 PM 1
Share

$$anonymous$$aybe debug if the nearRailPoint is the point what you need, you can create a simple static class on a C# script

 public static class HelperSpawn{
 
     public static void Add$$anonymous$$arker(Vector3 pos, Quaternion rot)
     {
         GameObject parent = new GameObject("$$anonymous$$arker");
         parent.transform.position = pos;
         parent.transform.rotation = rot;
 
         GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
         sphere.transform.SetParent(parent.transform);
         sphere.transform.localPosition = Vector3.zero;
         sphere.transform.localScale = new Vector3 (1.5f, .1f, 1.5f);
         GameObject prism = GameObject.CreatePrimitive(PrimitiveType.Cube);
         prism.transform.SetParent (parent.transform);
         prism.transform.localPosition = Vector3.up;
         prism.transform.localRotation = Quaternion.Euler (36f, 45, -45f);
 
         parent.transform.localScale = new Vector3 (.5f, 1f, .5f);
 
 
         $$anonymous$$aterial transparent$$anonymous$$aterial = new $$anonymous$$aterial (Shader.Find ("Standard"));
         transparent$$anonymous$$aterial.color = new Color (0f, 0.635294f, 1f, 0.482353f);
         sphere.GetComponent<$$anonymous$$eshRenderer>().material = transparent$$anonymous$$aterial;
         prism.GetComponent<$$anonymous$$eshRenderer>().material = transparent$$anonymous$$aterial;
     }
 
 }

this will spawn a (fancy?) marker, used to easy debug positions

you can add the following code just after assign nearRailPoint

 HelperSpawn.Add$$anonymous$$arker (nearRailPoint.transform.position, Quaternion.identity);

to know if the raycast is pointing to the correct objetive, also, maybe force is very little and the player don't do a visible transition , multiply force by a great number (`1000` is enough maybe) to see if the position of the player change

EDIT: I dont know if this really would help, probably all your scripts are in js, so you can develop a similar marker there or use a prefab, or some like

    Debug.DrawLine((nearRailPoint.transform.position, transform.position)

the goal is discover if nearRailPoint is storing the position that you want to go

avatar image knuckles209cp DCordoba · May 18, 2019 at 03:01 AM 0
Share

Wow thanks for the reply, Thats alot to take in! I will give this a go tonight when i get back from work, I just apply this script to the player correct?

Thank you in advance!

0 Replies

· Add your reply
  • Sort: 

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

225 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

Disabling a lineRender or Ray when there is no target 1 Answer

Raycast collided object not transforming when scripted to? 1 Answer

RaycastAll - get entry and exit points? 3 Answers

How to get info of object within certain range?(Javascript) 1 Answer

Raycast object as far as possible? 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