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 /
  • Help Room /
avatar image
0
Question by VAN-D00M · Feb 16, 2016 at 01:42 PM · camera-movementdirectioncamera-lookmovements

Character facing direction of movement - movement using camera transform?

Hello

This is the code we are using to move the player. The idea being the controls are relative to the cameras position as the camera changes (similar to FEZ fashion).

Problem is now we have this working, we need the players character to face the direction it is moving in. We are currently trying with the movement on the parent object and attempting to rotate the child character object. If that makes sense.

alt text

This is the movement script. void Update () {

         //Jump
         if (Input.GetKeyDown (KeyCode.Space) && (doubleJump < 2)) {
             rb.AddForce (new Vector3 (0, 1, 0) * jumpforce);
             doubleJump++;
         }
 
 
 
         //movement
         if (Input.GetKey (KeyCode.UpArrow)) 
         {
             movement = transform.position += Camera.main.transform.forward * speed;
             hammy.transform.LookAt(movement);
         }
         if (Input.GetKey (KeyCode.DownArrow)) 
         {
             movement = transform.position -= Camera.main.transform.forward * speed;
             hammy.transform.LookAt(movement);
         }
         if (Input.GetKey (KeyCode.LeftArrow)) 
         {
             movement = transform.position -= Camera.main.transform.right * speed;
             hammy.transform.LookAt(movement);
         }
         if (Input.GetKey (KeyCode.RightArrow)) 
         {
             movement = transform.position += Camera.main.transform.right * speed;
             hammy.transform.LookAt(movement);
         }
 
 

This is the camera follow script

 void LateUpdate()
     {
         
         //Follows the Players Position
         parent.transform.position = target.transform.position;
         
         //Lerps and chnages the cameras positon relevent to the Player
         transform.position = Vector3.Lerp (transform.position, currentCamera.transform.position, Time.deltaTime * 1.5f);
         transform.rotation = Quaternion.Lerp (transform.rotation, currentCamera.transform.rotation, Time.deltaTime * 1.5f);
         
         
         
         
     }



We have spent hours trying to figure this out.

Any help would be greatly appreciated. Thanks

capture.jpg (182.4 kB)
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 Salmjak · Feb 16, 2016 at 02:19 PM

the direction can be described as the normalized difference between two points.

 Vector3 direction = (target.position - your.position).normalized;
     Quaternion look = Quaternion.LookRotation(direction);
     transform.rotation = look;

Your target.position would be "movement". You probably also want to lerp the rotation (as you already do in your script). If you have the camera as a child of this object it wont work since childs will always rotate along with its parent (so if the character is the child it will also rotate with parent). If this is the case the best you can do is to separate them.

Comment
Add comment · Show 2 · 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 VAN-D00M · Feb 17, 2016 at 01:50 PM 0
Share

Thanks for the quick response @Salmjak. We just tried it and it works but we move like a side scroller left to right. Is there a similar solution or something else we can use to normalise?

avatar image Salmjak VAN-D00M · Feb 17, 2016 at 03:12 PM 0
Share

If you just move like a side-scroller you can just multiply a scale-component by -1f :) Ex. transform.scale.z = transform.scale.z*1f; This will flip the model. I'm not sure if you can edit it directly, if not, just save the scale in a temporary Vector3. Ex. Vector3 tempVec = new Vector3(transform.scale.x, transform.scale.y, transform.scale.z*-1f)

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

36 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

Related Questions

Scripting beginner - how can I make the camera follow the character but still face the same direction? 2 Answers

(Solveed) How to make object face same direction as its parent? 0 Answers

Trying to Make a Smooth Camera Zoom While Changing Targets 1 Answer

Camera to follow one object and rotate towards another 1 Answer

Camera Jitters When Displacing and Rotating Smoothly 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