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 mxdestro · Jul 17, 2018 at 05:14 AM · mouselookfps controllerwalls

FPS Controller and crawling on Walls/Ceiling

Hello!

Im having an issue converting this script: https://answers.unity.com/questions/155907/basic-movement-walking-on-walls.html to something that is compatible with a mouselook based camera/movement (an FPS basically). My desired behavior is something like the Alien in the AVP games

Here's my slightly modified code of the above example, its almost there except rotating the player character based off mouse input isnt working correctly. I think the issue is with the myTransform.Rotate? but I'm not sure what to do to fix it.

Heres the code so far:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 /// <summary>
 /// C# translation from http://answers.unity3d.com/questions/155907/basic-movement-walking-on-walls.html
 /// Author: UA @aldonaletto 
 /// </summary>
 
 // Prequisites: create an empty GameObject, attach to it a Rigidbody w/ UseGravity unchecked
 // To empty GO also add BoxCollider and this script. Makes this the parent of the Player
 // Size BoxCollider to fit around Player model.
 
 public class WallClimb : 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 = 10; // vertical jump initial speed
     private float jumpRange = 10; // range to detect target wall
     private Vector3 surfaceNormal; // current surface normal
     private Vector3 myNormal; // character normal
     private float distGround; // distance from character position to ground
     private bool jumping = false; // flag &quot;I'm jumping to wall&quot;
     private float vertSpeed = 0; // vertical jump current speed
 
     private Transform myTransform;
     public BoxCollider boxCollider; // drag BoxCollider ref in editor
     private Rigidbody myRigid;
     public Camera myCamera;
 
     private void Start()
     {
         myRigid = GetComponent<Rigidbody>();
         myNormal = transform.up; // normal starts as character up direction
         myTransform = transform;
         myRigid.freezeRotation = true; // disable physics rotation
                                          // distance from transform.position to ground
         distGround = boxCollider.extents.y - boxCollider.center.y;
 
     }
 
     private void FixedUpdate()
     {
         // apply constant weight force according to character normal:
         myRigid.AddForce(-gravity * myRigid.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
                 myRigid.velocity += jumpSpeed * myNormal;
             }
         }
 
 
         float horizontalAxis = Input.GetAxis("Horizontal");
         float verticalAxis = Input.GetAxis("Vertical");
 
         Vector3 forward = myCamera.transform.forward;
         Vector3 right = myCamera.transform.right;
 
         forward.y = 0f;
         right.y = 0f;
         forward.Normalize();
         right.Normalize();
 
         Vector3 desiredMoveDirection = forward * verticalAxis + right * horizontalAxis;
 
 
         // movement code - turn left/right with Horizontal axis:
         myTransform.Rotate(0, Input.GetAxis("Mouse X") * turnSpeed * Time.deltaTime, 0);
         // 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(Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime, 0, Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime);
        //myTransform.Translate(desiredMoveDirection * moveSpeed * Time.deltaTime);
     }
 
     private void JumpToWall(Vector3 point, Vector3 normal)
     {
         // jump to wall
         jumping = true; // signal it's jumping to wall
         myRigid.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));
         //jumptime
     }
 
     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);
             yield return null; // return here next frame
         }
         myNormal = normal; // update myNormal
         myRigid.isKinematic = false; // enable physics
         jumping = false; // jumping to wall finished
 
     }
 
 }
 



Comment
Add comment · Show 3
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 madks13 · Jul 17, 2018 at 12:03 PM 0
Share

If i remember right, when playing as an Alien and walking on walls, the camera was rotating so that the wall became ground (appearing on the down side of the screen)?

Does that mean your camera stays in the same rotation as walking on the ground and the wall is still on the left/right side of the screen?

avatar image mxdestro madks13 · Jul 17, 2018 at 07:24 PM 0
Share

well with this script the effect is similar since the entire player object rotates to the wall, so it looks like the wall is the ground below you, but I think what you're getting at is just faking it with the camera basically (and keeping the player object itself upright)?

avatar image madks13 mxdestro · Jul 18, 2018 at 09:24 AM 0
Share

What i'm doing is trying to understand what doesn't work. You say it works but doesn't. Can you give more details on how it doesn't work? If your entire player object rotates, but the camera doesn't follow properly, does that mean the camera isn't a child object of the player? The usual FPS games place the player camera as a child object of the player so the camera automatically follows the player object, and rotates with it. Are you doing the same or trying to manually move the camera?

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

86 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

Related Questions

Get a float from a different GameObject? (MouseLook) 1 Answer

DragRigidBody: Use mouse instead of camera position 0 Answers

GetComponent Problems with Networked FPS System 0 Answers

Anyone have a sensitivity slider script/tutorial used for an fps (using Unity's mouselook controller?) 1 Answer

First Person Player 3 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