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 God_of_Koolaid · Mar 11, 2021 at 04:42 AM · player movementcamera followcamera movement

Third person camera gets closer to the player

I have a third person controller that follows my player and the player moves forward in which ever direction my camera is facing. The problem is that when ever I start spamming the "W" and the jump button at around the same time, the camera starts to move closer to player, the camera even passes the player! to the point where he is not even on the screen, it could be 1 mile away from my camera. Not only does the camera pass the player, the player seems to be rotated to the right? or maybe it appears that way because of the close shot of the player, idk. Maybe parented the wrong things? I'll show you the playerMovement code, let me know if you need to see the camera look code.

Here's the player movement code: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerMovement : MonoBehaviour {

 public CharacterController controller;

 public float walkSpeed = 12f;
 public float jumpHeight = 100f;
 public float sprintSpeed = 18f;
 public float backwardsSpeed = 4f;
 public float currentSpeed = 0f;

 Animator anim;


 public float gravity = -9.81f;

 Vector3 velocity;
 Vector3 moveDirection;

 public Transform groundDetector;
 public float groundDistance = 0.4f;
 public LayerMask groundMask;

 bool isGrounded;

 void Start()
 {
     controller = GetComponent<CharacterController>();
     anim = GetComponentInChildren<Animator>();
 }


 // Update is called once per frame
 void Update()
 {
     Move();
 }

 void Move()
 {
     isGrounded = Physics.CheckSphere(groundDetector.position, groundDistance, groundMask);

     float moveX = Input.GetAxis("Horizontal");
     float moveZ = Input.GetAxis("Vertical");


     //Plays Walking Animation
     if (Input.GetKey(KeyCode.W))
     {
         anim.SetInteger("movementCondition", 1);
     }
     else
     {
         anim.SetInteger("movementCondition", 0);
     }


     if (isGrounded && velocity.y < 0)
     {
         velocity.y = -2f;
     }

     //Jump Input
     if (isGrounded && Input.GetKeyDown(KeyCode.Space))
     {
         velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
     }


     //Sprint if player is holding left-shift and "W" at the same time, should return to walking speed when left-shift is let go
     moveDirection = new Vector3(moveX, 0, moveZ);
     Debug.Log(moveDirection);

     //Sprinting
     if (Input.GetKey(KeyCode.LeftShift) && moveZ == 1 && isGrounded)
     {
         currentSpeed = sprintSpeed;
     }
     else
     {
         currentSpeed = walkSpeed;
     }

     //Moving Backwards
     if (moveZ == -1)
     {
         currentSpeed = backwardsSpeed;
     }

     Debug.Log(currentSpeed);

     Vector3 move = transform.right * moveX + transform.forward * moveZ;

     controller.Move(move * currentSpeed * Time.deltaTime);

     velocity.y += gravity * Time.deltaTime;

     controller.Move(velocity * Time.deltaTime);
 }


}

Comment
Add comment · Show 6
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 GeroNL · Mar 11, 2021 at 07:21 AM 0
Share

Where is the camera?

avatar image God_of_Koolaid GeroNL · Mar 11, 2021 at 09:36 AM 0
Share

Here's the CameraLook Code: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class MouseLook : MonoBehaviour { Vector3 cameraPosition;

 public float mouseSensitivity = 200f;

 public float maxLookDown = 90;
 public float maxLookUp = -90;

 public Transform playerBody;

 float xRotation = 0;

 // Start is called before the first frame update
 void Start()
 {
     Cursor.lockState = CursorLockMode.Locked;
 }

 // Update is called once per frame
 void Update()
 {
     float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
     float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

     xRotation -= mouseY;
     xRotation = Mathf.Clamp(xRotation, maxLookDown, maxLookUp);
     //xRotation = Mathf.Clamp(xRotation, -90, 90);

     transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
     playerBody.Rotate(Vector3.up * mouseX);
avatar image God_of_Koolaid God_of_Koolaid · Mar 11, 2021 at 09:42 AM 0
Share
 Here's what the Hierarchy looks like. At first I programmed the game to be First Person, but then I didn't like the outcome, so I just moved the camera out of the mesh to make it look like it was third person. My "Mouse Look" script is in the Main Camera, and my "Player Movement" script is in the "First Person player", which is a capsule.


alt text

screenshot-2021-03-11-013749.png (8.6 kB)
Show more comments

0 Replies

· Add your reply
  • Sort: 

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

118 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

Related Questions

How to get the camera's size in world units 1 Answer

2D game. How do I make the camera follow the player until it reaches the edge of a section of the map? 0 Answers

Cinemachine & Playmaker 3rd person zoom 0 Answers

How to make a camera Follow an Object moving in zigzag path? 1 Answer

Camera Moves or Player Moves for SideScroller 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