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 Jardaran · Apr 07, 2020 at 07:34 AM · camera-movementplayer movementbeginners

need help with player movement for 3rd person project

hi, really new to unity and coding as a whole. trying to learn the basics so i can put together a simple game, at this point i have a cube as a player that has basic movement and jumping with a bool to ensure no double jumps. iv gotten a camera working which can rotate around the player with joysticks or a mouse as id like this to work with a controller. the camera also has some basic collision but iv noticed that the movement of the player is relative to the world axis not the direction the camera is facing which id prefer eg: forward movement should be whichever direction the camera is facing. to get this far i have combined a few tutorials for each bit of the project. if someone could take a look at my code and help me get the movement working properly i would be very grateful, would love a decent explanation of what it is you did to help so i can learn from this. 2 of my 3 scripts are here, my playermovement and camerafollow, didnt include the collision script as i highly doubt it would be relevant here, if it is let me know and ill add it. Thanks in advance

first script is my camera control script

second is the script for my player controller

 public class CameraFollow : MonoBehaviour
 {
     public float CameraMoveSpeed = 120f;
     public GameObject CameraFollowObject;
     Vector3 FollowPosition;
     public float ClampAngle = 80f;
     public float InputSensitivity = 150f;
     public GameObject CameraObject;
     public GameObject PlayerObject;
     public float CameraDistaneXToPlayer;
     public float CameraDistaneYToPlayer;
     public float CameraDistaneZToPlayer;
     public float MouseX;
     public float MouseY;
     public float FinalInputX;
     public float FinalInputZ;
     public float SmoothX;
     public float SmoothY;
     private float RotationY = 0f;
     private float RotationX = 0f;
 
    
     // Start is called before the first frame update
     void Start()
     {
         Vector3 rot = transform.localRotation.eulerAngles;
         RotationY = rot.y;
         RotationX = rot.x;
         Cursor.lockState = CursorLockMode.Locked;
         Cursor.visible = false;
     }
 
     // Update is called once per frame
     void Update()
     {
         //this sets up the rotation of the sticks/mouse
         float InputX = Input.GetAxis("RightStickHorizontal");
         float InputZ = Input.GetAxis("RightStickVertical");
         MouseX = Input.GetAxis("Mouse X");
         MouseY = Input.GetAxis("Mouse Y");
         FinalInputX = InputX + MouseX;
         FinalInputZ = InputZ + MouseY;
 
         RotationY += FinalInputX * InputSensitivity * Time.deltaTime;
         RotationX += FinalInputZ * InputSensitivity * Time.deltaTime;
 
         RotationX = Mathf.Clamp(RotationX, -ClampAngle, ClampAngle);
 
         Quaternion localRotation = Quaternion.Euler(RotationX, RotationY, 0.0f);
         transform.rotation = localRotation;
 
     }
 
     private void LateUpdate()
     {
         CameraUpdater();
     }
 
     void CameraUpdater()
     {   //sets the target object to follow
         Transform target = CameraFollowObject.transform;
 
         //move towards the game object that is the target
         float step = CameraMoveSpeed * Time.deltaTime;
         transform.position = Vector3.MoveTowards(transform.position, target.position, step);
     }
 }


 public class PlayerMovement: MonoBehaviour
 {
     public float PlayerSpeed;
     public float JumpHeight;
     private Rigidbody Player;
     private bool IsGrounded;
 
 
     void Start()
     {
         Player = GetComponent<Rigidbody>();
     }
 
 
     void Update()
     {
         Player.velocity = new Vector3(Input.GetAxis("Horizontal") * PlayerSpeed, Player.velocity.y, Input.GetAxis("Vertical") * PlayerSpeed);
 
         if (Input.GetButtonDown("Jump"))
         {
             if (IsGrounded == true)
             Player.velocity = new Vector3(Player.velocity.x, JumpHeight, Player.velocity.z);
         }
     }
 
     private void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.tag == "Ground")
         {
             IsGrounded = true;
             Debug.Log("grounded");
         }
     }
     private void OnCollisionExit(Collision collision)
     {
         if (collision.gameObject.tag == "Ground")
         {
             IsGrounded = false;
             Debug.Log("ungrounded");
         }
     }
 }
 






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

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

134 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

Related Questions

Simple Third Person Camera-Relative Movement with WASD - kind of working but need a fix 2 Answers

Camera script for ball not working as intended, looking for some tips, or advise. 0 Answers

Camera problem 2 Answers

Trying to get the camera to orbit, but also steer the player when moving. 0 Answers

Rigidbody movement in direction of camera? 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