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
1
Question by Cheaterman24 · Apr 14, 2015 at 10:30 PM · camerarotationmovementplayer movement

How to make my player look in the direction of its movement?

I've looked around the forums for an answer for this question, but haven't found one that works, yet.

My player moves in relation to the direction of the camera, i.e. forward = camera.forward, right = camera.right etc.

I've tried using things like setting the transform.rotation to Quaternion.LookRotation, but that messes up the movement really bad.

I've been tearing my hair over this, maybe one of you guys can help me.

Here's my code for the movement (all in Update):

 maxSpeed = 0.25f

 float forwardAxis = Input.GetAxis ("Vertical");
 float sideAxis = Input.GetAxis ("Horizontal");
 Vector3 forward = (camera.transform.forward * forwardAxis) * maxSpeed;
 Vector3 sideways = (camera.transform.right * sideAxis) * maxSpeed;
 forward.y = 0;
 sideways.y = 0;
 movement = new Vector3 (sideways.x + forward.x, sideways.y+forward.y, sideways.z + forward.z);
 transform.Translate (movement);


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

3 Replies

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

Answer by Cheaterman24 · Apr 15, 2015 at 07:13 PM

I've actually solved the problem I had myself.

In the camera script I am using, I wanted something a little better than just childing the camera to the player, but the way I fixed the problem was making the model a child of another empty gameobject and only applying the movement to the parent gameobject. I then apply the rotation to the child model(using Quaternion.LookRotation() ), and it makes the player look in the direction of movement without affecting the movement.

Comment
Add comment · 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
0

Answer by YoungDeveloper · Apr 14, 2015 at 10:31 PM

If you are moving towards specific vector3 point you can use Transform.MoveTowards().

Comment
Add comment · 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
0

Answer by toddisarockstar · Apr 15, 2015 at 01:26 AM

is this JS or C#????

actually, its easier than you think. in this case, you make the camera a parent of the character you want to move. Then the camera simply follows position and rotation of the object automatically without code.

simply drag and drop the camera into the player character and it will follow him. horray!

after that, the answer above me from (young developer) is correct. your verticle axis will pry rotate though.

post your entire code and we can fix.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Script that stays behind player that rotates the player along with the camera/mouse (third person) 0 Answers

Camera Rotating when the player reach a specific position 0 Answers

How to smoothly rotate camera around an object with touch 1 Answer

Choppy movement of moving quad with rotating camera 1 Answer

What is difference between local and global coord Vector3. 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