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 pennzo · Mar 01, 2021 at 09:28 PM · movementrigidbodyrotation axis

Player Rotation and movement

I'v been trying to add rotation and movement towards the way the character is facing, without breaking the physics or locking the character. After hours of googling/youtube and experimentation on how to implement this im back for help again >_<. This is the closest the closest i got to it actually working.The character is rotating but its drifting and moving to the way he's facing but the rotation is awful.. Can someone expainto me what im doing wrong and what the best solution is. This is my first script and im trying to understand and figure things out.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerMovement : MonoBehaviour {

 //VARIABLES
 
   //MOVEMENT//SPEED
 [SerializeField]  private float moveSpeed;
 [SerializeField]  private float walkSpeed;
 [SerializeField]  public float runSpeed;
 [SerializeField]  public float rot;
 [SerializeField]  public float rotSpeed;
 [SerializeField]  public float smooth;
 public float turnSmoothTime = 0.1f;
 
 
   //GRAVITY
 [SerializeField] private float jumpHeight;
 [SerializeField] private float gravity;
 public bool isGrounded = true;
 //    [SerializeField] private float groundCheckDistance; 
  //[SerializeField] private LayerMask groundMask; 
   // Variable for Layer for groundcheck
 
 private Vector3 moveDirection;
 private Vector3 velocity;
 private Animator anim;
 Rigidbody rb;
 
 
 //REFERENCES
 private CharacterController controller; 
 
 private void Start()
 {
     //Debug.Log("Start");
     controller = GetComponent<CharacterController>();
     anim = GetComponent<Animator>();
     rb = GetComponent<Rigidbody>();
         }
 
 

   void OnCollisionEnter(Collision theCollision)
  {
      if (theCollision.gameObject.tag == "Plane")
      {
          isGrounded = true;
          print("grounded");
                  }
  }
 
 
  void OnCollisionExit(Collision theCollision)
  {
      if (theCollision.gameObject.tag == "Plane");
      {
          isGrounded = false;
           print("not grounded");
      }
  }
 private void Update()
 {
     //rot += Input.GetAxisRaw("Mouse X") * rotSpeed * Time.deltaTime;
         //transform.eulerAngles = new Vector3(0, rot, 0);
      //rotation
     

     
                         
     //Debug.Log("test");
     Move();
     
 }

 private void fixedUpdate()

 {

 }

 
     
 //MOVEMENT
 private    void Move()
 {       
     
         
                     
         float moveZ = Input.GetAxisRaw("Vertical");
         float ad = Input.GetAxis("Horizontal");
         //float rot = Input.GetAxis("Mouse X");
            
         
                
             moveDirection = new Vector3(ad, rb.velocity.y, moveZ);
             
         
         if(isGrounded == true)  
         {
             
                                                  
              
             if(moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift)) 
             
             {
                 
                 //Walk
                 Debug.Log("Walk");
                 walk();
             }    
             else if(moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
             {
                 
                 anim.SetFloat("JumpAnim", 0); //Animation stop
                 //Run
                 run();
                 Debug.Log("run");
             }
             else if(moveDirection == Vector3.zero)
             {
                 //Ìdle
                 anim.SetFloat("JumpAnim", 0);
                 idle();
                 
             }    

             if (Input.GetButtonDown("Jump") && isGrounded)
              {
                  //Animation stop
                  //Jump
                  jump();
                  
              }

     
          
         }
         
         //transform.rotation =Quaternion.LookRotation(transform.up * rotSpeed * rot);
         //rb.AddTorque(transform.up * rotSpeed * rot);
         //float targetAngle = Mathf.Atan2(moveDirection.x, moveDirection.z) * Mathf.Rad2Deg;
         //float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle,ref smooth,turnSmoothTime);
         //transform.rotation = Quaternion.Euler(0f,angle,0f);
         float rot = Input.GetAxis("Mouse X");
         moveDirection = transform.TransformDirection(moveDirection);
         rb.AddTorque(transform.up * rotSpeed * rot);
         
            
         
         rb.velocity = new Vector3(moveDirection.x * moveSpeed, rb.velocity.y, moveDirection.z * moveSpeed);
         
         
         
     //transform.rotation = Quaternion.Euler(0f,0,0f);
 }
 
 private    void idle()
 {
     Debug.Log("Idle");
     float MMY = Input.GetAxis("Vertical");
     anim.SetFloat("InputY",MMY );  //
 }    
 private    void walk() /////WALK 
     {
         moveSpeed = walkSpeed;
         anim.SetFloat("run", 0);
         Debug.Log("walk");
     float MMX = Input.GetAxis("Horizontal");
     float MMY = Input.GetAxis("Vertical");
     
     
     anim.SetFloat("InputY",MMY);
     anim.SetFloat("InputX",MMX);
 }

 private    void run() ///// RUN
 {
     
         moveSpeed = runSpeed;
         anim.SetFloat("run", 1);
         float MMX = Input.GetAxis("Horizontal");
         float MMY = Input.GetAxis("Vertical");
         anim.SetFloat("InputY",MMY);
         anim.SetFloat("InputX",MMX);

 }
 private void jump()
 {
     anim.SetFloat("JumpAnim", 1);
     Debug.Log("jump");
      //rb.AddForce(0, 0, jumpHeight, ForceMode.Impulse);

      rb.AddForce((Vector3.up) * jumpHeight, ForceMode.Impulse);
     // rb.AddForce(0, 0, jumpHeight, ForceMode.Impulse);
     //velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); //
     

 }
  
   

 

 
 

}

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

245 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

Related Questions

Realistic Soccer Ball Dribbling? 1 Answer

stop the ball from moving after button is pressed 1 Answer

Everything that moves need an Rigid Body? 1 Answer

Rigidbody movement instead of transform? 0 Answers

Respawn objects 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