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 developerguy91 · May 22, 2016 at 03:38 AM · rotation1st personridgidbody

Custom character controller randomly rotates after colliding with an object

My character controller after colliding with an any object adds continuous rotation on the y.

I have no idea whats happening...

Motor Script:

 using UnityEngine;
 using System.Collections;
 using System;
 
 [RequireComponent(typeof(Rigidbody))]
 public class FirstPerson3DMotor : MonoBehaviour {
 
     private Rigidbody rb;
 
     private Vector3 _velocity = Vector3.zero;
     private Vector3 _rotation = Vector3.zero;
     private Quaternion targetRotation;
     private float camRotation;
     private float currentCameraRotationX = 0f;
 
     private float rotationLimit = 80f;
 
     private Camera cam;
 
     // Use this for initialization
     void Start () {
         rb = GetComponent<Rigidbody>();
 
         cam = GetComponent<FirstPerson3DController>().playerCamera;
     }
 
     // Fixed Update is called every time a physics event (iteration) occurs.
 
     void FixedUpdate ()
     {
         ApplyMovement();
         ApplyRotation();
     }
 
     void ApplyRotation()
     {
 
         rb.MoveRotation(rb.rotation * Quaternion.Euler(_rotation));
 
         if (cam != null)
         {
 
                 currentCameraRotationX -= camRotation;
                 currentCameraRotationX = Mathf.Clamp(currentCameraRotationX, -rotationLimit, rotationLimit);
 
                 cam.transform.localEulerAngles = new Vector3(currentCameraRotationX, 0f, 0f);
 
         }
     }
 
     void ApplyMovement()
     {
         if (_velocity != Vector3.zero)
         {
             rb.MovePosition(rb.position + _velocity * Time.fixedDeltaTime);
         }
     }
 
     public void Move(Vector3 velocity)
     {
         _velocity = velocity;
     }
 
     public void Rotate(Vector3 rotation)
     {
         _rotation = rotation;
     }
 
     public void RotateCamera(float cameraRotation)
     {
         camRotation = cameraRotation;
     }
 
     public void Jump(float jumpHeight)
     {
         if(Input.GetKeyDown(KeyCode.Space))
         {
             rb.velocity.y.Equals(jumpHeight);
         }
     }
 }

Controller Script:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 [RequireComponent(typeof(FirstPerson3DMotor))]
 public class FirstPerson3DController : MonoBehaviour {
 
     //used to represent the speed the character walks.
     [Header("Movement Settings")]
     public float walkSpeed = 2.0f;
 
     //Used to represent their speed while sprinting.
     public float moveSpeed;
 
     //Used to represent look sensitivity for the camera.
     public float lookSensitivity = 10.0f;
 
     //Used for setting the jump height
     public float jumpHeight = 4f;
 
     //Used to represent if the player can sprint or not. 
     //(Usage: if the player is hurt and they cannot sprint).
     public bool canSprint = true;
 
     //Used to represent weather the player's footsteps can be heard.
     [Header("Sound Settings")]
     public bool soundEnabled = true;
 
     //used to represent the clip-clop sound of footsteps.
     [SerializeField]
     private AudioClip footStep1;
 
     [SerializeField]
     private AudioClip footStep2;
 
     [Header("HUD Settings")]
     public bool enableHUD = true;
     public GameObject reticle;
     public bool hideReticle = false;
     
     [Header("Player Camera")]
     public Camera playerCamera;
 
     private FirstPerson3DMotor fpsMotor;
 
     private bool isSprinting;
     private bool isJumping;
 
     private GameObject playerHud;
 
     // Called when the game starts.
     void Start () {
 
         /*Setting FPS Motor equal to the the object this 
         scripts attached to's version of the FPS Motor Object. */
         fpsMotor = GetComponent<FirstPerson3DMotor>();
 
         playerHud = GetComponentInChildren<Canvas>().gameObject;
 
         Cursor.visible = false;
 
         HUDConfigure();
 
         Physics.gravity = new Vector3(0, -20.0F, 0);
 
         moveSpeed = walkSpeed * 1.5f;
     }
 
     void HUDConfigure()
     {
         if (enableHUD == false)
         {
             playerHud.SetActive(false);
         }
 
         if(hideReticle == true)
         {
             reticle.SetActive(false);
         }
     }
 
     void HideCursorListener()
     {
 
         if(Input.GetKeyDown(KeyCode.Escape))
         {
             Cursor.visible = true;
         }
 
         if (Input.GetMouseButtonDown(0))
         {
             Cursor.visible = false;
         }
 
     }
 
 
     void CheckIfSprinting()
     {
         if (Input.GetKeyDown(KeyCode.LeftShift) && canSprint == true)
         {
             isSprinting = true;
         }
 
         if(Input.GetKeyUp(KeyCode.LeftShift))
         {
             isSprinting = false;
         }
 
     }
 
     void CalculateMovementVelocity()
     {
         float xMove = Input.GetAxis("Horizontal");
         float zMove = Input.GetAxis("Vertical");
 
         Vector3 movHorizontal = transform.right * xMove;
         Vector3 movVertical = transform.forward * zMove;
 
         Vector3 velocity;
 
         if (isSprinting == false)
         {
             velocity = (movHorizontal + movVertical) * walkSpeed;
         }
         else
         {
             velocity = (movHorizontal + movVertical) * moveSpeed;
         }
 
         fpsMotor.Move(velocity);
 
         if (Input.GetKeyDown(KeyCode.Space))
         {
            GetComponent<Rigidbody>().AddForce(Vector3.up * jumpHeight, ForceMode.VelocityChange);
         }
 
 
 
     }
 
     void CalculateRotation()
     {
         float _yRot = Input.GetAxisRaw("Mouse X");
 
         Vector3 _rotation = new Vector3(0f, _yRot, 0f) * lookSensitivity;
 
         //Apply rotation
         fpsMotor.Rotate(_rotation);
 
         //Calculate camera rotation as a 3D vector (turning around)
         float _xRot = Input.GetAxisRaw("Mouse Y");
 
         float cameraRotation = _xRot * lookSensitivity;
 
         //Apply camera rotation
         fpsMotor.RotateCamera(cameraRotation);
     }
 
 
     void Update () {
 
         CheckIfSprinting();
 
         CalculateMovementVelocity();
         CalculateRotation();
 
         HideCursorListener();
         
     }
 }

alt text

Thanks in advance :)

Also disregard the is kinematic tick box being clicked I did that for debugging purposes.

ccsettings.png (41.6 kB)
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Friction seems to be slowing my rolling ball instead of helping it accelerate. How to improve my rotation-based movement? 1 Answer

Flip over an object (smooth transition) 3 Answers

Rotate static collider, Is it too bad for preformance? 1 Answer

How to move player in direction where the camera is aiming ? 1 Answer

Limiting X-Axis rotation to 90 degrees and -90 degrees 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