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 antiquote · Aug 03, 2013 at 07:24 PM · rotationjavascriptgameobjectmove

Trying to rotate and move a gameobject through JavaScript, help?

Hi, I'm trying to rotate and move the player through JavaScript into a bed. I'm using the First Person Controller. There's a problem though. I have no idea how! I looked up some stuff on Answers and Google, but it was either it totally confused me or it wasn't something I was looking for. So can someone try write this for me? Explanation is GREATLY appreciated. Thanks for the help.

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 aldonaletto · Aug 03, 2013 at 08:05 PM

A somewhat easy way to make simple animations like this via script is to create an empty game object and place it at the desired position and rotation, then lerp to the position/rotation when needed. You should also have another empty game object defining the stand up position, so that you could lerp to it when leaving the bed.

The code would be something like this:

 public var bedPos: Transform; // drag here the empty that defines the bed target position/rotation
 public var bedDuration: float = 0.6; // duration in seconds
 public var upPos: Transform; // drag here the empty that sets the stand up position/rotation
 public var upDuration: float = 0.6;
 
 private var moving = false;
 
 function LerpMe(target: Transform, duration: float): IEnumerator {
   var pos0 = transform.position;
   var rot0 = transform.rotation;
   var t: float = 0;
   moving = true; // start moving now
   while (t < 1){
     t += Time.deltaTime/duration;
     transform.position = vector3.Lerp(pos0, target.position, t);
     transform.rotation = Quaternion.Slerp(rot0, target.rotation, t);
     yield;
   }
   moving = false; // movement finished
 }

You can move the player to bed with this call (player script):

 if (!moving){ // do nothing if already moving
   LerpMe(bedPos, bedDuration);
 }

And the player can wake up like this (player script):

 if (!moving){
   LerpMe(upPos, upDuration);
 }

NOTE: You can rotate the transform to any angle, but remember that the CharacterController itself doesn't rotate - it's always upright, aligned to the world Y. It's only the character collider that refuses to rotate, thus visually there's nothing wrong - except for the green capsule that you can see in the Scene view: it will remain up and alert while the player sleeps like a log. This may cause some problems: the player may fall trough the terrain case the bottom of the capsule penetrates the ground in the bed position, for instance. Due to this and other possible problems, it's a good idea to disable the movement script while going to bed, and enable it again when completely in the up position.

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 antiquote · Aug 04, 2013 at 05:08 AM 0
Share

Thanks A LOT. This was very well explained. $$anonymous$$ore than I intended actually. Thank you for pointing out the possible bugs, and telling me how it's done so I can use it in the future. Also sorry for the long wait for a response. I had to go to work xD.

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

15 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

Related Questions

Rotate an Object 1 Answer

Link object rotation to its parent 1 Answer

Rotating an object in position 2 Answers

How do you flip a card with smooth rotation? 1 Answer

using a custom rotation in "Instantiate" 4 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