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 RedSuinit · Dec 11, 2012 at 02:08 AM · 2dwallstick

Trying to make my 2D Character Controller stick to a wall.

Hey all, I'm trying to write my own 2D platformer controller. I found some code to start from that had a basic left and right movement with jumping. I added the info for double jumping, and now I am trying to make it so that the character will "stick" to a specific wall type, and slowly slide down it until you "jump off". There should be no control over the character until you jump off the wall. I'm really struggling with this.

using UnityEngine; using System.Collections;

public class PlatformerController : MonoBehaviour {

public float Gravity = 20f; public float StickyGravity = 20f; public float TerminalVelocity = 20f; public float TerminalVelocitySet = 20f; public float JumpSpeed = 6f; public float DoubleJumpSpeed = 6f; public float MoveSpeedSet = 10f; public float MoveSpeed = 10f; private float jumpCount = 0;

public Vector3 MoveVector {get; set;} public float VerticalVelocity {get; set;}

public CharacterController CharacterController;

// Use this for initialization void Awake () { CharacterController = gameObject.GetComponent("CharacterController") as CharacterController; }

// Update is called once per frame void Update () {

checkMovement(); HandleActionInput(); processMovement(); }

void checkMovement(){ //move l/r var deadZone = 0.1f; VerticalVelocity = MoveVector.y; MoveVector = Vector3.zero; if(Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone){ MoveVector += new Vector3(Input.GetAxis("Horizontal"),0,0); } //jump

}

void HandleActionInput(){ if(CharacterController.isGrounded){ jumpCount = 0; } if(jumpCount < 1){ if(Input.GetButtonDown("Jump")){ jump();

} } else{ if(jumpCount == 1){ if(Input.GetButtonDown("Jump")){ jumpjump(); } } } }

void processMovement(){ //transform moveVector into world-space relative to character rotation MoveVector = transform.TransformDirection(MoveVector);

//normalize moveVector if magnitude > 1 if(MoveVector.magnitude > 1){ MoveVector = Vector3.Normalize(MoveVector); }

//multiply moveVector by moveSpeed MoveVector *= MoveSpeed;

//reapply vertical velocity to moveVector.y MoveVector = new Vector3(MoveVector.x, VerticalVelocity, MoveVector.z);

//apply gravity applyGravity();

//move character in world-space CharacterController.Move(MoveVector * Time.deltaTime);

if(RaycastHit.stickywall){ wallstick (); } }

void applyGravity(){ if(MoveVector.y > -TerminalVelocity){ MoveVector = new Vector3(MoveVector.x, (MoveVector.y - Gravity * Time.deltaTime), MoveVector.z); } if(CharacterController.isGrounded && MoveVector.y < -1){ MoveVector = new Vector3(MoveVector.x, (-1), MoveVector.z); } }

public void jump(){ if(CharacterController.isGrounded){ VerticalVelocity = JumpSpeed; jumpCount++; } } public void jumpjump(){ VerticalVelocity = DoubleJumpSpeed; jumpCount++; } void wallstick(){ TerminalVelocity = 0; MoveSpeed = 0; if(Input.GetButtonDown("Jump")){ MoveVector += new Vector3(JumpSpeed, JumpSpeed/10, 0); if(CharacterController.isGrounded){ MoveVector += new Vector3(MoveVector.x, MoveVector.y, MoveVector.z); } } } }

Can anyone help?

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

9 People are following this question.

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

Related Questions

How to fix player bumping into the wall? 0 Answers

Find distance within screen! 2 Answers

Unity 2D Platformer Enemy flipping 1 Answer

Basic 2D cannon rotation code not working 1 Answer

How to make it so enemies only move towards the player when the player is colliding with an object 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