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 /
  • Help Room /
avatar image
0
Question by stuartrdowling · Oct 23, 2018 at 09:01 PM · transformmovement scriptscriptingproblem

Issue with player moving forward through collider

Hi all, This is my first time posting to this forum, so thanks in advance to all who read and hopefully respond. Im slowly following a tutorial to create a climbing system for a third person game, i wish to use the basic functions of this as the basis of a climbing game. I'm trying to get the player to climb a simple cube, the controls for movement are GetInputAxis, hor and vert, i can see they are returning values in the debug log. The character is correctly Lerping to the face of the cube at a predefined height, however as soon as I move the arrow keys the player is moving in the correct direction but also moving forward and getting stuck in the cube object. I've spent two days trying to make changes to affect this but I'm having no joy. I understand some but not all of the C# code that I've been building (with the help of a brilliant Sharp Accent tutorial - BOTW Climbing) The player should be moving relative to the face of the cube, but instead the player is always moving forward when i press up, down, left or right and then freezing.

Any help greatly appreciated! i guess i just want to know how to disable the player from moving forward into a collider when the player is in climbing position.

Thanks!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 namespace SA
     
 {
 
 public class FreeClimb : MonoBehaviour 
 
 {
         
         public bool isClimbing;
         public Animator anim;
         public bool inPosition;
         public bool isLerping;
         float t;
         Vector3 startPos;
         Vector3 targetPos;
         Quaternion startRot;
         Quaternion targetRot;
         public float positionOffset;
         public float offsetFromWall = 0.45f;
         public float speed_multiplier = 0.2f;
         public float climbSpeed = 3;
         public float rotateSpeed = 5;
         public float inAngleDis = 1;
         
         
         public float horizontal;
         public float vertical;
         
         Transform helper;
         float delta;
         
                 
         
     void Start()
         
     {
         Init();
     }
 
     public void Init()
 
     {
             helper = new GameObject().transform;
             helper.name = "climb helper";
             
             CheckForClimb();
     }
         
     public void CheckForClimb()
     
     {
         Vector3 origin = transform.position;
         origin.y += 1.4f;
         Vector3 dir = transform.forward;
         RaycastHit hit;
         if(Physics.Raycast(origin,dir, out hit, 2))
         {
             helper.position = PosWithOffset(origin, hit.point);
             InitForClimb(hit);
         }
     }
         
     void InitForClimb(RaycastHit hit)
         
 {
             isClimbing = true;
                 helper.transform.rotation = Quaternion.LookRotation(-hit.normal);
                 startPos = transform.position;
                 targetPos = hit.point + (hit.normal * offsetFromWall);
                 t = 0;
                 inPosition = false;
                 anim.CrossFade("climb_idle", 1);
 }
             
             void Update()
             {
                 delta = Time.deltaTime;
                 Tick(delta);
             }
 
 
     
             public void Tick(float delta)
             {
                 if(!inPosition)
                 {
                     GetInPosition();
                     return;
                 }
                 
                 if(!isLerping)
                 {
                     horizontal = Input.GetAxis("horizontal");
                     vertical = Input.GetAxis("vertical");
                     float m = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
                     
                     Vector3 h = helper.right * horizontal;
                     Vector3 v = helper.up * vertical;
                     Vector3 moveDir = (h + v).normalized; 
 
                     
                     bool canMove=CanMove(moveDir);
                     if (!canMove || moveDir == Vector3.zero)
                         return;
                     
                     
                     t = 0;
                     isLerping = true;
                     startPos = transform.position;
                     Vector3 tp = helper.position - transform.position;
                     targetPos = helper.position;
                     
                 }
                 
                 else
                 {
                     t += delta * climbSpeed;
                     if(t > 1)
                     {
                         t = 1;
                         isLerping = false;
                     }
                     
                     Vector3 cp = Vector3.Lerp(startPos, targetPos, t);
                     transform.position = cp;
                     transform.rotation = Quaternion.Slerp(transform.rotation, helper.rotation, delta * rotateSpeed);
                 }
             }
                 
              bool CanMove(Vector3 moveDir)
             { 
                  Debug.Log(moveDir);
                  Vector3 origin = transform.position;
                 float dis = positionOffset;
                 Vector3 dir = moveDir;
                 Debug.DrawRay(origin, dir * dis, Color.red);
                 RaycastHit hit;
                 
                 if(Physics.Raycast(origin,dir, out hit ,dis))
                     
                 {
                     return false;
                 }
                 
                 origin += moveDir * dis;
                 dir = helper.forward;
                 float dis2 = inAngleDis;
                 
                 Debug.DrawRay(origin, dir * dis2, Color.blue);
                 if(Physics.Raycast(origin,dir, out hit, dis))
                 {
                     helper.position = PosWithOffset(origin, hit.point);
                     helper.rotation = Quaternion.LookRotation(-hit.normal);
                     Debug.Log("f");
                     return true;
                 }
                 
                 origin += dir * dis2;
                 dir = Vector3.up;
                 
                 Debug.DrawRay(origin, dir, Color.yellow);
                 if(Physics.Raycast(origin,dir, out hit, dis2))
                 {
                     float angle = Vector3.Angle(helper.up, hit.normal);
                     if(angle < 40)
                     {
                         helper.position = PosWithOffset (origin, hit.point);
                         helper.rotation = Quaternion.LookRotation(-hit.normal);
                         return true;
                     }
                 }
                 
                 return false;
             }
             
             
             void GetInPosition()
             {
                 t += delta;
                 
                 if(t > 1)
                 
                 {
                     t = 1;
                     inPosition = true;
                 }
                 
                 Vector3 tp = Vector3.Lerp(startPos, targetPos, t);
                 transform.position = tp;
                 transform.rotation = Quaternion.Slerp(transform.rotation, helper.rotation, delta * rotateSpeed);
             }
             
                 Vector3 PosWithOffset (Vector3 origin, Vector3 target)
             {
                 Vector3 direction = origin - targetPos;
                 direction.Normalize();
                 Vector3 offset = direction * offsetFromWall;
                 return target + offset;
                 
                 }
             }
 }
 
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

184 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

Related Questions

how to make the player move in only 2 directions? 0 Answers

LineRenderer disables/overides Transform 1 Answer

My code is not cycling through an array as expected 1 Answer

RogueLike 2D (Tutorial) Rotation of Player 1 Answer

Limit movement to just forward/backward and left/right 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