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 Jcavanaugh · Mar 17, 2015 at 03:49 PM · c#camerarotationcharacter

How to reset character orientation based on direction the camera is facing

A friend of mine and I are attempting to create a first person multiplayer game. We currently have our characters able to load and join the server, as well as a camera that moves with the mouse. However, the characters orientation is consistent no matter what direction the camera is facing. (I.E. When facing backwards and trying to move forwards, the character is going backwards in perspective to the camera.) What code can I add in C# to set my characters orientation to the cameras facing direction?

EDIT: Here's my current code that I have attached to the player. It is a multiplayer network. The code did not originate from me, we used a tutorial to get to this point. Now, we are attempting to edit it from a third person view where the camera was immobile into a first person view. I am using the default MouseLook script to move the camera.

 public class Player1 : MonoBehaviour
 {
     public float speed = 10f;
 
     private float lastSynchronizationTime = 0f;
     private float syncDelay = 0f;
     private float syncTime = 0f;
     private Vector3 syncStartPosition = Vector3.zero;
     private Vector3 syncEndPosition = Vector3.zero;
 
     void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
     {
         Vector3 syncPosition = Vector3.zero;
         Vector3 syncVelocity = Vector3.zero;
         if (stream.isWriting)
         {
             syncPosition = rigidbody.position;
             stream.Serialize(ref syncPosition);
 
             syncPosition = rigidbody.velocity;
             stream.Serialize(ref syncVelocity);
         }
         else
         {
             stream.Serialize(ref syncPosition);
             stream.Serialize(ref syncVelocity);
 
             syncTime = 0f;
             syncDelay = Time.time - lastSynchronizationTime;
             lastSynchronizationTime = Time.time;
 
             syncEndPosition = syncPosition + syncVelocity * syncDelay;
             syncStartPosition = rigidbody.position;
         }
     }
 
 
     void Awake()
     {
         lastSynchronizationTime = Time.time;
     }
 
     void Update()
     {
         if (networkView.isMine)
         {
             InputMovement();
             InputColorChange();
         }
         else
         {
             SyncedMovement();
         }
 
     }
 
 
     private void InputMovement()
     {
         if (Input.GetKey(KeyCode.W))
             rigidbody.MovePosition(rigidbody.position + Vector3.forward * speed * Time.deltaTime);
 
         if (Input.GetKey(KeyCode.S))
             rigidbody.MovePosition(rigidbody.position - Vector3.forward * speed * Time.deltaTime);
 
         if (Input.GetKey(KeyCode.D))
             rigidbody.MovePosition(rigidbody.position + Vector3.right * speed * Time.deltaTime);
 
         if (Input.GetKey(KeyCode.A))
             rigidbody.MovePosition(rigidbody.position - Vector3.right * speed * Time.deltaTime);
 
         if (Input.GetKey (KeyCode.Space))
             rigidbody.MovePosition (rigidbody.position + Vector3.up * speed * Time.deltaTime);
         {
             transform.forward = Camera.main.transform.forward;
         }
     }
 
     private void SyncedMovement()
     {
         syncTime += Time.deltaTime;
 
         rigidbody.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
     }
 
 
     private void InputColorChange()
     {
         if (Input.GetKeyDown(KeyCode.R))
             ChangeColorTo(new Vector3(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f)));
     }
 
     [RPC] void ChangeColorTo(Vector3 color)
     {
         renderer.material.color = new Color(color.x, color.y, color.z, 1f);
 
         if (networkView.isMine)
             networkView.RPC("ChangeColorTo", RPCMode.OthersBuffered, color);
     }
 }
 
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 AlwaysSunny · Mar 17, 2015 at 02:36 PM 0
Share

Welcome to UA, Please show us code and/or tell us what you've tried when posting questions. Where prudent, screenshots or diagrams always help.

I don't understand the scenario you're trying to describe, and it's likely others won't either. Please re-phrase your question, either by editing if you can, or posting a comment here.

Are you using the "first person controller" script and CharacterController component from StandardAssets?. Are you using the stock $$anonymous$$ouseLook script to drive the camera? Take a look at the FPS prefab from StandardAssets for an example of a working FPS controller.

Gotta be super descriptive around here. ;)

1 Reply

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

Answer by Cherno · Mar 17, 2015 at 04:55 PM

Change Vector3.forward (... right, up etc.) to transform.forward (... right, up etc.).

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 Jcavanaugh · Mar 17, 2015 at 05:21 PM 0
Share

Amazing, thank you! Worked beautifully. As you can probably tell by how simple that seems, I am a beginner. Only reason I got that far was the tutorial told me what to use.

avatar image Cherno · Mar 17, 2015 at 05:32 PM 0
Share

Glad to be of help. Feel free to mark the answer aas accepted if it indeed helped you with your problem.

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

Flip over an object (smooth transition) 3 Answers

Move camera certain distance away from object, but keep object centered (with specific camera rotation) 0 Answers

Character Rotation 2 Answers

Camera to follow the player in the form of radius 1 Answer

Problem with Camera rotation script 3 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