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 vankempenfrancois · Sep 06, 2018 at 09:39 AM · rotationcamera-movementcamera rotatecamera-lookcamera follow

Camera following player gameObject,Camera follows player, with script attached.

I have a camera inside a player object, and that player has a script that rotates it when the mouse is moved along the X axis. The camera has a script that rotates it when the mouse is moved along the Y axis. The camera usually follows the player along the X axis, but when I enable this script it doesn't. I can put the Y axis code in the player's script, but that rotates the whole player. I can put the X axis code in both, but the camera always ends up faster than the player, even when they have the same speed set.


Here are the scripts


Script for the player

 public class PlayerMovement : MonoBehaviour {
 
   public float speed = 20.0F;
   public float jumpSpeed = 8.0F;
   public float gravity = 20.0F;
   private Vector3 moveDirection = Vector3.zero;
   private CharacterController controller;
   private Animator anim;
 
   public float speedH = 10.0f;
 
   private float yaw = 0.0f;
 
   void Start()
   {
       controller = GetComponent<CharacterController>();
       anim = GetComponent<Animator>();
   }
   void Update() 
   {
         
     if (controller.isGrounded) 
     {      
       moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
       moveDirection = transform.TransformDirection(moveDirection);
       moveDirection *= speed;
 
       if (Input.GetButton("Jump"))
       {
         moveDirection.y = jumpSpeed;
       }
 
              yaw += speedH * Input.GetAxis("Mouse X");
 
       transform.eulerAngles = new Vector3(0.0f, yaw, 0.0f);
     }
     moveDirection.y -= gravity * Time.deltaTime;
 
     controller.Move(moveDirection * Time.deltaTime);
     
     }
 }



Script for camera

 public class Look : MonoBehaviour {
 
   public float speedV = 10.0f;
   public float maxUp = 60f;
   public float maxDown = -60f;
 
   private float pitch = 0.0f;
 
     void Update () 
     {
     pitch -= speedV * Input.GetAxis("Mouse Y");
 
     pitch = Mathf.Clamp(pitch, maxDown, maxUp);
 
     transform.eulerAngles = new Vector3(pitch, 0.0f, 0.0f);    
     }
 }
 


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
1
Best Answer

Answer by RocketFriday · Sep 06, 2018 at 10:32 AM

When you parent an object to another object it automatically inherits all rotation. So when your player rotates with the x-axis from the mouse, the camera will too. By adding the x-axis code to the camera you're adding extra rotation on top of that.

as for the issue with it not rotating after enabling the script.


don't do:

transform.eulerAngles = new Vector3(pitch, 0.0f, 0.0f);

instead do:

transform.eulerAngles = new Vector3(pitch, transform.eulerAngles.y, 0.0f);

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 vankempenfrancois · Sep 06, 2018 at 10:50 AM 0
Share

Legend, thanks.

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

127 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

Related Questions

Making object Clamp to Look Rotation of Camera 1 Answer

Automatic 3rd person camera 1 Answer

Free Orbit Cam and Mouse Aim Cam aren't working together 0 Answers

Camera rotation 0 Answers

Automatic Camera Rotation in the 3D Game Kit 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