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 Dennis 4 · Nov 04, 2010 at 09:06 PM · animationmovementraycasttransform

Possible to transform and objects position smoothly with confined movement dictated by raycasting, but controlled by user?

What I'm trying to create is a control scheme something along the lines of Boxi. Basically I have a sphere that the user will click on, and then drag in the direction they want the sphere to go. However that will have no bearing on the translation of the sphere. Instead the sphere is to calculate how far it is from the next collider and then automatically and smoothly animate towards that coordinate(confined to an axis) and stop before it actually collides with the wall. This is what I have to find the distance and re-position, but I'm not sure how to animate the transform smoothly:

var hit: RaycastHit; var smooth : int;

function Update (){ var horiz: float = Input.GetAxis("Mouse X"); var vert: float = Input.GetAxis("Mouse Y"); if(rigidbody.velocity.magnitude == 0){ if(Input.GetMouseButton(0) && horiz >= .5) { if (Physics.Raycast(transform.position, Vector3.right, hit)){ var distanceToRight = hit.distance; transform.Translate(Vector3(Mathf.FloorToInt(distanceToRight - .5),0,0)); } }if(Input.GetMouseButton(0) && horiz <= -.5) { if (Physics.Raycast(transform.position, -Vector3.right, hit)){ var distanceToLeft = hit.distance; transform.Translate(Vector3(Mathf.RoundToInt(-distanceToLeft + 1),0,0)); } }if(Input.GetMouseButton(0) && vert >= .5) { if (Physics.Raycast(transform.position, Vector3.forward, hit)){ var distanceToTop = hit.distance; transform.Translate(Vector3(0,0,Mathf.FloorToInt(distanceToTop - .5))); } }if(Input.GetMouseButton(0) && vert <= -.5) { if (Physics.Raycast(transform.position, -Vector3.forward, hit)){ var distanceToBottom = hit.distance; transform.Translate(Vector3(0,0,Mathf.RoundToInt(-distanceToBottom + 1))); } } } }

Should I write another script that has an Update function to transform the spheres position? I've tried and it doesn't seem to work properly.

Any insight would be greatly appreciated. Thanks! Dennis

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 Kroltan · Jan 02, 2012 at 06:43 PM 0
Share

Have you experimented with the Vector3.Lerp() function? It seems pretty useful in this case. You get the original position and the raycast hit position, then lerp the object to that position. Of course in your specific case you would need to calculate a distance of the wall to not collide with it. The only cons i can think now is that Lerp() does not include physics, so yous sphere would only slide. Such behavior would only be important in textured meshes, but you could use a UV animation too.

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

1 Person is following this question.

avatar image

Related Questions

Player moves too fast and won't move to point 1 Answer

Enemy Animation to not stop when player runs out of range 1 Answer

Learning to move 1 Answer

Can I animate objects/cameras in Update() without breaking Physics.Raycast? 1 Answer

Controlling a rigged models muscle groups via script (C#) 0 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