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 /
avatar image
0
Question by Sam2ertheBOSS · May 12, 2018 at 01:05 AM · scripting problemunity5

Camera follow

I want my mouse to be able to rotate the main camera around the player (up and down), but when I try to do it, it glitches and will not let me. I can do it horizontally, but not vertically . See this to understand my problem

  1. I have attached my main camera to my player object.
  2. There is no camera follow script.
  3. I want my camera to rotate around my player
  4. there is no rigid body, I am using a character controller

public class PlayerMovement : MonoBehaviour {

 public float moveSpeed;
 public Transform target;
 public float jumpForce;
 public CharacterController cc;
 private Vector3 moveDirection;
 public float gravityScale;
 public float Xsensitivity;
 public float Ysensitivity;
 public Transform Pivot;
 public Camera camera;
 private Vector3 rotateValue;
 

 void Start(){
     
     camera = Camera.main;
     
     camera.enabled = true;
     cc = GetComponent<CharacterController>();
     target.GetComponent<Transform>();
     Pivot.transform.position = target.transform.position;
     Pivot.transform.parent = target.transform;
     Cursor.lockState = CursorLockMode.Locked;
 }

 void FixedUpdate(){
     float h = Input.GetAxis("Horizontal");
     float v = Input.GetAxis("Vertical");
     float hor = Input.GetAxis("Mouse X") * Xsensitivity;
     target.Rotate(0.0f, hor, 0.0f);
     float vert = Input.GetAxis("Mouse Y") * Ysensitivity;
     Pivot.Rotate(-vert, 0, 0);
     rotateValue = new Vector3(hor, vert * -1, 0.0f);
     camera.transform.eulerAngles = transform.eulerAngles - rotateValue;



     float yStore = moveDirection.y;
     moveDirection = (transform.forward * v) + (transform.right * h);
     moveDirection = moveDirection.normalized * moveSpeed;
     moveDirection.y = yStore;
     if (cc.isGrounded){

         moveDirection.y = 0.0f;
         if (Input.GetButtonDown("Jump"))
         {
             moveDirection.y = jumpForce;
         }

     }
     
     moveDirection.y += (Physics.gravity.y*gravityScale*Time.deltaTime);
     cc.Move(moveDirection*Time.deltaTime);
 }

}

THank you in advance :)

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 iJuan · May 12, 2018 at 01:18 AM

First of all:

Inputs should never be on FixedUpdate

If you put inputs in FixedUpdate(), there's a chance you will miss some frames.


Said that, your camera should be at LateUpdate, so all changes on that frame will be reflected. Once that's done, your camera should work, but it will probably bug your CharacterController (The player should always me moved before the camera does)

I'd recommend you to manage player's Input in your Character controller and Camera's Input in a Camera Controller, don't try to mix them.

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

155 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to rotate spine by the script, while an animation from the animator is playing? 0 Answers

where have i went wrong in my script (unity 5) 0 Answers

2 character interaction animation 2 Answers

enable and disable loop objects in the scene 1 Answer

Hello All. How do you access the Run Speed, Jump Speed...ect variables in Unity 5s' new First Person Controller?? 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