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 Bruno Gysin · Dec 04, 2014 at 07:40 AM · 2dmovementrigidbody2dmove

adjust the player in the ground And movements

Hi

how to catch the 'direction' of the vertex/polygon to apply to the character? the point of it be adjusted to the ground.

Exemple:

alt text

the ground in green is only a picture with component polygon collider.

Question 2:

using a simple script that uses rigidbody2d to move the player,I found some problems: using rigidbody the player slide When it gets on a mountain. to climb a mountain is more difficult than to descend. is there any alternative for this? or the rigidbody2d? ("put the seriousness of the player in the direction of the object could solve this")

Comment
Add comment · Show 1
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 sysameca · Dec 04, 2014 at 08:05 AM 0
Share

I have done this before. But you have to forget about using unity's physics here.

--Raycast from the player towards the collider.

--Get the colliders perpendicular normal.

--From here the perpendicular normal should be adjusted to the correct player movement direction.

--And finally rotate the character and move it accordingly to the correct perpendicular normal vector.

I can post you a code example later tonight, because i just don't have enough time now. But by that time maybe somebody will post you a code sample as an answer.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jenci1990 · Dec 04, 2014 at 09:59 AM

 //C#
 void OnCollisionStay (Collision col) {
     if (col.collider.gameObject.tag == "Ground") { //Check the collider is ground
         transform.up = col.contacts[0].normal;
     }
 }
 
 //JS
 function OnCollisionStay (col : Collision) {
     if (col.collider.gameObject.tag == "Ground") { //Check the collider is ground
         transform.up = col.contacts[0].normal;
     }
 }
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 sysameca · Dec 05, 2014 at 08:33 AM

Ok. I made a short example of what you are trying to achieve. What you need is a ground with some 2D collider and a Player object with this script only. I still don't have enough time to polish it, but you will get the picture:

 using System;
 using System.Collections.Generic;
 using System.Collections;
 using UnityEngine;


 public class Player : MonoBehaviour
 {
 private void Update()
 {
     RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector3.down, Mathf.Infinity);

     // Get the perpendicular normal
     Vector3 direction = new Vector3(hit.normal.y * -1, hit.normal.x);
     // Move right
     if (Input.GetKey(KeyCode.D))
     {
         transform.position += direction * -1 * Time.deltaTime;
         transform.localScale = new Vector3(1, transform.localScale.y, transform.localScale.z);
         transform.rotation = Quaternion.LookRotation(Vector3.forward, hit.normal);
     }
     // Move left
     else if (Input.GetKey(KeyCode.A))
     {
         transform.position += direction * Time.deltaTime;
         transform.localScale = new Vector3(-1, transform.localScale.y, transform.localScale.z);
         transform.rotation = Quaternion.LookRotation(Vector3.forward, hit.normal);
     }
 }
 }

This example does not rely on physics therefore you need to implement your own rules for gravity and other stuff, but the result can be observed when you hit play. In real good platformer kind of game it's good to set your own rules even if it's harder at first.

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 Bruno Gysin · Dec 05, 2014 at 02:43 PM 0
Share

I tested your script, but it is so: http://somevid.com/0AL$$anonymous$$eQnBJdCj$$anonymous$$nlv8BGV

....

avatar image sysameca · Dec 05, 2014 at 07:37 PM 0
Share

Yes indeed the rotation was not correct. I was rushing in this morning. Check it out now it is reupdated.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rigidbody2D x velocity not moving unless placed in FixedUpdate 1 Answer

Movement restriction... 1 Answer

How do I make my character's movement less floaty? 1 Answer

Make an object that moves relative to the touch more natural 0 Answers

Unity 2d mobile movement 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