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 TheTimeLord · May 30, 2012 at 09:44 PM · mouselookwalkingwallsonwith

Walking On Walls With Mouselook!

Hi! I just started using unity recently and I already figured out how to do a few simple things. My problem is as follows: I would like a script that would make the character be able to walk on walls (in other words, change gravity to the opposite of the character's normal) all while being able to use mouselook as well. I found this code, which looks and works great, except for the fact that the "a" and "d" keyes turn the character instead of making it sidestep. Any help would be appreciated! Here's the code I found earlier. Thanks in advance! :D By the way, the script is added to an empty which has rigid body and a box collider.

 var moveSpeed: float = 8; // move speed
 var turnSpeed: float = 90; // turning speed (degrees)
 var gravity: float = 10; // character gravity
 var isGrounded: boolean;
 var deltaGround: float = 0.2; // character is grounded up to this distance
 var jumpSpeed: float = 10; // vertical jump initial speed
 var jumpRange: float = 10; // range to detect target wall
 
 private var myNormal: Vector3; // character normal
 private var turnAngle: float = 0; // current character direction
 private var distGround: float; // distance from character position to ground
 private var jumping = false; // flag "I'm jumping to wall"
 private var vertSpeed: float = 0; // vertical jump current speed 
 
 function Start(){
 
     myNormal = transform.up; // starting character normal 
     rigidbody.freezeRotation = true; // disable physics rotation
     // distance from transform.position to ground
     distGround = collider.size.y / 2 - collider.center.y;  
 }
 
 function FixedUpdate(){
     // apply constant weight force
     rigidbody.AddForce(-gravity * myNormal);
 }
 
 function Update(){
 
     var ray: Ray;
     var hit: RaycastHit;
 
     if (!jumping){ // does nothing while jumping to wall
         if (Input.GetButtonDown("Jump")){ // jump pressed:
             ray = Ray(transform.position, transform.forward);
             if (Physics.Raycast(ray, hit, jumpRange)){ // wall ahead?
                 JumpToWall(hit.point, hit.normal); // yes: jump to the wall
             }
             else if (isGrounded){ // no: if grounded, jump up
                 vertSpeed = jumpSpeed;
             }                
         }
         else {
             var turn = Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime;
             turnAngle = (turnAngle + turn) % 360; // update character direction 
             ray = Ray(transform.position, -myNormal); // cast ray downwards
             if (Physics.Raycast(ray, hit)){ // use it to update myNormal and isGrounded
                 isGrounded = hit.distance <= distGround + deltaGround;
                 myNormal = Vector3.Lerp(myNormal, hit.normal, Time.deltaTime);
             }
             else {
                 isGrounded = false;
             }
             // rotate character to myNormal...
             var rot = Quaternion.FromToRotation(Vector3.up, myNormal);
             rot *= Quaternion.Euler(0,turnAngle,0); // and to current direction
             transform.rotation = rot;
             var move = Input.GetAxis("Vertical") * moveSpeed * Vector3.forward;
             move.y = vertSpeed; // include jump speed, if any
             transform.Translate(move * Time.deltaTime, Space.Self); // move the character
             vertSpeed -= gravity * Time.deltaTime; // apply gravity to jump, if any
             if (vertSpeed < 0) vertSpeed = 0; // clamp to zero
         }            
     }
 }
 
 function JumpToWall(point: Vector3, normal: Vector3){
     // jump to wall 
     jumping = true; // signal it's jumping to wall
     rigidbody.isKinematic = true; // disable physics while jumping
     var orgPos = transform.position;
     var orgRot = transform.rotation;
     var dstPos = point + normal * (distGround + 0.5); // will jump to 0.5 above wall
     var dir = dstPos - orgPos;
     var dstRot = Quaternion.FromToRotation(Vector3.up, normal);
     dstRot *= Quaternion.Euler(0, turnAngle, 0); // keep character direction
     for (var t: float = 0.0; t < 1.0; ){
         t += Time.deltaTime;
         transform.position = Vector3.Lerp(orgPos, dstPos, t);
         transform.rotation = Quaternion.Slerp(orgRot, dstRot, t);
         yield; // return here next frame
     }
     myNormal = normal; // update myNormal
     rigidbody.isKinematic = false; // enable physics
     jumping = false; // jumping to wall finished
 }
Comment
Add comment · Show 5
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
avatar image MiraiTunga · Feb 14, 2013 at 12:42 AM 0
Share

can you use lookAt while walking on the wall? iam using a similar script and cant use lookat while wall walking any tips?

avatar image drizztmainsword · Feb 14, 2013 at 12:58 AM 0
Share

LookAt makes it so that the Y axis of whatever transform you are affecting points as close to "up" in worldspace as possible.

If I had to guess, it's probably using Quaternion.SetLookRotation() to do this. You should be able to use this function to manipulate whatever it is you want to look in a specific direction and have the Y axis point towards the character's relative "up."

for example (not real code):

 transform.localRotation = Quaternion.LookRotation(directionToLookIn, transform.localRotation * Vector3.up);

You'll have to do some work to get the specifics, but that is most definitely the function you want to work with. Check it out in the Scripting Reference.

avatar image MiraiTunga · Feb 14, 2013 at 01:30 AM 0
Share

thanks for the hint this is what iam using now

 player.transform.localRotation = Quaternion.LookRotation(objectIWantlookat.transform.localPosition- player.transform.localPosition );

its working when i look at the object on the original floor but when i try look at an object on the wall i am currently walking on my character is just shaking

avatar image MiraiTunga · Feb 14, 2013 at 01:59 AM 0
Share

my actual question is on this link

avatar image MiraiTunga · Feb 16, 2013 at 06:29 AM 0
Share

Got it to work!! used raycast and Rotate

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by drizztmainsword · Jun 02, 2012 at 08:23 AM

Alright.

First, to disable rotation with the a and d keys, find these lines and comment them out:

 var turn = Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime;
 turnAngle = (turnAngle + turn) % 360; // update character direction

Now, to make it so that you strafe, find this line:

 var move = Input.GetAxis("Vertical") * moveSpeed * Vector3.forward;

And under it add:

 move.x = Input.GetAxix("Horizontal") * moveSpeed;

That should work. If you are strafing, but in opposite directions, add a "`*-1`" to the end of that line. If you are moving backwards and forwards (which shouldn't happen, but hey), change "`move.x`" to "`move.z`".

Comment
Add comment · Show 2 · 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
avatar image TheTimeLord · Jun 10, 2012 at 02:57 PM 0
Share

That's great, thanks! It works well, but there's one problem that I can't figure out. Everything works except for the horizontal mouse look. I can move my character forwards backwards, strafe and jump. It also does the gravity thing, and I can make the camera look up and down. The only thing it's not letting me do is rotating the character using the mouselook script. What could the problem be? Thanks in advance!

avatar image drizztmainsword · Jun 10, 2012 at 08:11 PM 0
Share

Right, so when you edited the script, we disabled the the horizontal axis. To get it to map to the mouse, you should be able to grab the horizontal mouse information from Input.

Try changing those first two lines you commented out to read like this:

var turn = Input.GetAxis("$$anonymous$$ouse X') turnSpeed Time.deltaTime; turnAngle = (turnAngle + turn) % 360;

That might be your ticket, though you'll probably have to tweak whatever the value of "turnSpeed" is. If turning with the mouse still feels strange, trying removing the " * Time.deltaTime" bit from that line and see if it works any better.

avatar image
0

Answer by TheTimeLord · Jun 11, 2012 at 01:45 AM

Thanks it worked great! I didn't realize it could have been something as small as one name but hey, that's code for you! :D

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

Answer by MykalDesign · Dec 07, 2017 at 03:25 AM

TheTimeLord.

Is there any way you could share the completed script set from this? I can not find a MouseLook that doesn't freak out once you shift native y rot.

Thank you !!!

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Walking on walls? 4 Answers

FPS walking inside of a cylinder 1 Answer

Unity 3D Java - FPC Side Walking Through The Wall Problem 0 Answers

Walking through walls 1 Answer

How to use lookAt() while my character walking on a wall ? 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