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 Smithy311 · Mar 03, 2015 at 09:17 AM · c#movementtransformmousemouselook

Unity's mouse look causing movement issues

Hi, the default mouse look script for unity is causing movement issues in my game.

I used some movement code and tweaked it to suit my game better, and the mouse look is causing it to bug when walking up a wall. It works fine when the mouse look script is off, but I need it on for the final mechanic of my game.

I've narrowed it down to one line of code within the mouse look script:

 transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0); // Causes bug


Movement script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class WalkTestcs : MonoBehaviour 
 {
     private float moveSpeed = 6; // move speed
     private float turnSpeed = 90; // turning speed (degrees/second)
     private float lerpSpeed = 10; // smoothing speed
     private float gravity = 10; // gravity acceleration
     private bool isGrounded;
     private float deltaGround = 0.2f; // character is grounded up to this distance
     private float jumpSpeed = 5; // vertical jump initial speed
     private float jumpRange = 5; // range to detect target wall
     private float walkRange = 1; // range to detect target wall when walking
     private Vector3 surfaceNormal; // current surface normal
     private Vector3 myNormal; // character normal
     private float distGround; // distance from character position to ground
     private bool jumping = false; //I'm jumping to wall"
     private float vertSpeed = 0; // vertical jump current speed
     private Transform myTransform;
     public BoxCollider boxCollider; // drag BoxCollider ref in editor
     
     private void Start()
     {
         myNormal = transform.up; // normal starts as character up direction
         myTransform = transform;
         rigidbody.freezeRotation = true; // disable physics rotation
         // distance from transform.position to ground
         distGround = boxCollider.extents.y - boxCollider.center.y;
         //GetComponent <Mouse>().enabled = !GetComponent <Mouse>().enabled;
     }
     
     private void FixedUpdate()
     {
         // apply constant weight force according to character normal:
         rigidbody.AddForce(-gravity*rigidbody.mass*myNormal);
     }
     
     private void Update()
     {
         // jump code - jump to wall or simple jump
         if (jumping) return; // abort Update while jumping to a wall
         
         Ray ray;
         RaycastHit hit;
         
         if (Input.GetButtonDown("Jump"))
         { // jump pressed:
             ray = new Ray(myTransform.position, myTransform.forward);
             if (Physics.Raycast(ray, out hit, jumpRange))
             { // wall ahead?
                 JumpToWall(hit.point, hit.normal); // yes: jump to the wall
             }
             else if (isGrounded){ // no: if grounded, jump up
                 rigidbody.velocity += jumpSpeed * myNormal;
             }
         }
         
         // movement code - turn left/right with Horizontal axis:
         myTransform.Rotate(0, Input.GetAxis("Horizontal")*turnSpeed*Time.deltaTime, 0);
         //transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime);
         // update surface normal and isGrounded:
         ray = new Ray(myTransform.position, -myNormal); // cast ray downwards
         if (Physics.Raycast(ray, out hit)){ // use it to update myNormal and isGrounded
             isGrounded = hit.distance <= distGround + deltaGround;
             surfaceNormal = hit.normal;
         }
         else 
         {
             isGrounded = false;
             // assume usual ground normal to avoid "falling forever"
             surfaceNormal = Vector3.up;
         }
         myNormal = Vector3.Lerp(myNormal, surfaceNormal, lerpSpeed*Time.deltaTime);
         // find forward direction with new myNormal:
         Vector3 myForward = Vector3.Cross(myTransform.right, myNormal);
         // align character to the new myNormal while keeping the forward direction:
         Quaternion targetRot = Quaternion.LookRotation(myForward, myNormal);
         myTransform.rotation = Quaternion.Lerp(myTransform.rotation, targetRot, lerpSpeed*Time.deltaTime);
         // move the character forth/back with Vertical axis:
         myTransform.Translate(0, 0, Input.GetAxis("Vertical")*moveSpeed*Time.deltaTime);
 
         if (Input.GetButton ("Vertical")) 
         {
             { // jump pressed:
                 ray = new Ray(myTransform.position, myTransform.forward);
                 if (Physics.Raycast(ray, out hit, walkRange)){ // wall ahead?
                     JumpToWall(hit.point, hit.normal); // yes: jump to the wall
                 }
             }
         }
     }
     
     private void JumpToWall(Vector3 point, Vector3 normal)
     {
         // jump to wall
         jumping = true; // signal it's jumping to wall
         rigidbody.isKinematic = true; // disable physics while jumping
         Vector3 orgPos = myTransform.position;
         Quaternion orgRot = myTransform.rotation;
         Vector3 dstPos = point + normal * (distGround + 0.5f); // will jump to 0.5 above wall
         Vector3 myForward = Vector3.Cross(myTransform.right, normal);
         Quaternion dstRot = Quaternion.LookRotation(myForward, normal);
         StartCoroutine (jumpTime (orgPos, orgRot, dstPos, dstRot, normal));
 
     }
     
     private IEnumerator jumpTime(Vector3 orgPos, Quaternion orgRot, Vector3 dstPos, Quaternion dstRot, Vector3 normal) 
     {
         for (float t = 0.0f; t < 1.0f; )
         {
             t += Time.deltaTime;
 
             myTransform.position = Vector3.Lerp(orgPos, dstPos, t);
             myTransform.rotation = Quaternion.Slerp(orgRot, dstRot, t);
             rigidbody.freezeRotation = true;
             yield return null; // return here next frame
         }
         myNormal = normal; // update myNormal
         rigidbody.isKinematic = false; // enable physics
         jumping = false; // jumping to wall finished
     }
 }

Any help would be great!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by lordlycastle · Mar 03, 2015 at 11:23 AM

It's because when using eulerAngles, you have to make sure that the angle is between 0º and 360º. Don't use them to increment, as it'll fail when it's more than 360. So you could make a private method like the following and call it before setting the angles.

 private void CheckAngles(ref Vector3 angles){
             if (angles.x > 360)
                 angles.x -= 360;
             else if (angles.x < 0)
                 angles.x += 360;
 
             if (angles.y > 360)
                 angles.y -= 360;
             else if (angles.y < 0)
                 angles.y += 360;
 
             if (angles.z > 360)
                 angles.z -= 360;
             else if (angles.z < 0)
                 angles.z += 360;
     }

Also I see that you are using Transform.Rotate to rotate left or right. If you just want to rotate around one axis, it better to use Transform.RotateAround.

 transform.RotateAround(transform.position, Vector3.up, Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime);


Comment
Add comment · Share
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

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

21 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

a better movement code C# 3 Answers

Make player face same direction as camera. 4 Answers

How to rotate Character on Y axis along with the mouse rotation? 0 Answers

Mouse Swipe and Code Efficiency 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