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 /
  • Help Room /
avatar image
0
Question by issdar · Jun 23, 2020 at 12:56 PM · cameracamera-movementplayer movementcamera rotate

,Third Person Movement Script Help

Hello everyone! I have been trying to get this to work for three days banging my head against the wall :( I have gotten it to work for the most part by piecing other people scripts and tutorials together. I would like my character to rotate in place when stationary and pressing any button on the horizontal axis. Also when I run forward and press horizontal axis would like character to gradually turn while running. Except if the rightmouse button is held down then strafe left and right if pressing the horizontal axis. This probably is confusing and I can draw it out if need be. Below is my current code and if anyone has a suggestion to get ride of the nasty if statement "if(horizontal != 0 || vertical != 0)" on line 72 that would be awesome. Thanks in advance for any help I appreciate it. I am also using cinemachine free look camera to control the camera for the player set to heading target forward and orbit world space. playermovementscript.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Mirror;
 
 public class ThirdPersonMovement : NetworkBehaviour
 {
     public CharacterController controller;
     public Animator animator;
     public Transform cam;
     public AudioListener myAudioListener;
     InputActions inputActions;
 
     // public floats editable in GUI
     public float mouseSensitivity = 3f, playerSpeed = 50f;
     public float jumpForce = 1f;
     public float gravity = 10f;
     public float turnSmoothTime = 0.1f;
     public float turnSpeed = 8.0f;
     
     private float rotSpeed = 80;
 
     private float rot = 0f;
 
     // private variables not editable in GUI
     private float vertVelocity;
     private Vector2 movementInput;
     private Vector3 direction;
     private Vector3 stationaryCheck;
     private float turnSmoothVelocity;
     private bool hasJumped;
 
     private bool rightMouseInput = false;
 
     void Awake()
     {
         inputActions = new InputActions();
         inputActions.Player.Move.performed += ctx => movementInput = ctx.ReadValue<Vector2>();
         inputActions.Player.Camera.performed += ctx => rightMouseInput = ctx.ReadValueAsButton();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (!isLocalPlayer)
         {
             // exit from update if this is not the local player
             return;
         }
 
         PlayerMovement();
 
         if(inputActions.Player.Jump.triggered)
         {
             hasJumped = true;
         }
 
         ApplyGravity();
     }
 
     void PlayerMovement()
     {
         // set variables for inputActions move performed (WASD Keys)
         float horizontal = movementInput.x;
         float vertical = movementInput.y;
         direction = new Vector3(horizontal, vertVelocity, vertical);
         
         // finds the angle of where camera is pointing and smooth turn
         float targetAngle = Mathf.Atan2(0, 0) * Mathf.Rad2Deg + cam.eulerAngles.y;
         float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
         
         if(horizontal != 0 || vertical != 0)
         {
             transform.rotation = Quaternion.Euler(0f, angle, 0f);
         }
         
         direction = Quaternion.Euler(0f, targetAngle, 0f) * direction;
         
         controller.Move(direction * playerSpeed * Time.deltaTime);
 
         if(vertical > 0 && controller.isGrounded)
         {
             animator.SetFloat("Walk", 1);
         }else{animator.SetFloat("Walk", 0);}
     }
     
     void ApplyGravity()
     {
         // applies a consistent 9.1f gravity if 
         if (controller.isGrounded == true)
         {
             if(hasJumped == false){
                 vertVelocity = Physics.gravity.y;
             }else{
                 vertVelocity = jumpForce;
             }
         }else{
             // consistenly increase speed falls
             vertVelocity += Physics.gravity.y * Time.deltaTime;
             vertVelocity = Mathf.Clamp(vertVelocity, -50f, jumpForce);
             hasJumped = false;
             animator.SetFloat("Walk", 0);
         }
     }
 
     void AnimationControls()
     {
         
     }
 
     void OnEnable() {
         inputActions.Enable();
     }
 
     void OnDisable() {
         inputActions.Disable();
     }
 }
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

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

Locking Camera's rotation on a rolling character 0 Answers

Help With Camera rotation on X and Y Axis (Target Focused) 1 Answer

Making the camera rotation in line with the ball 1 Answer

viewing on the Mouse Y axis not working. 0 Answers

How to have your player controls change while your camera rotates? 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