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 lbryan212 · Oct 05, 2018 at 08:34 PM · transform.positionraycasthittime.deltatime

Transform player to RaycastHit.point depending on speed float

I'm working on a Third Person cover system shooter and currently I'm working on the cover functionality.


I have two scripts: a character controller script and a cover detection script.


I have code on my cover detection script that detects cover with a raycast and creates a raycasthit.point at the point of collision with the collider. I also have a float on the same script called coverMoveSpeed that I'd like to multiply against time.delta so the player doesn't instantly snap to the cover, instead I'd like it to smoothly move there depending on the coverMoveSpeed float.


In my head I thought it would be as simple as this:

 bool coverKey = Input.GetButton("Cover");
 
         if (coverKey & coverMovement.canCover)
         {
 
             transform.position = coverMovement.hit.point + coverMoveSpeed * Time.deltaTime;
         }
         else
         {
             
         }

Here's my implementation

 //on my cover detect script
 
 public float coverMoveSpeed = 3.0f;


 //Here's my character controller script
 
 bool coverKey = Input.GetButton("Cover");
 
         if (coverKey & coverMovement.canCover)
         {
 
             transform.position = coverMovement.hit.point;
         }
         else
         {
             
         }


However it doesnt compile, I know I'm close so I'm turning to the unity forums once again!


beep beep "Paging Dr. Stark" @LCStark

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

1 Reply

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

Answer by LCStark · Oct 05, 2018 at 09:14 PM

transform.position = coverMovement.hit.point + coverMoveSpeed * Time.deltaTime;
This will not compile - you are trying to add two different data types: coverMovement.hit.point which is a Vector3, and coverMoveSpeed * Time.deltaTime, which is a float.

You want to move your transform from its current position ( transform.position) to the point where you found the collision with the cover ( coverMovement.hit.point ? ). You can use Vector3.MoveTowards function to do that. (Vector3.MoveTowards documentation)

As you can see in the example given in the Scripting API documentation, it's pretty simple:

void Update() {
    // The step size is equal to speed times frame time.
    float step = speed * Time.deltaTime;
// Move our position a step closer to the target. transform.position = Vector3.MoveTowards(transform.position, target.position, step); }
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 lbryan212 · Oct 05, 2018 at 10:49 PM 0
Share

Once again, you're a great tutor!

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

95 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

Related Questions

RayCastHit 2 Answers

moving objects with transform position 2 Answers

Changing the position of an object hit with a RaycastHit -1 Answers

Select Object and move on Right click Position - Error! 0 Answers

2D - Move an object 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