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 henriquenj · Jun 23, 2010 at 10:15 PM · charactercontrollerplatform2d-gameplaysync

Issue on moving using CharacterController

Hi everyone.

I have a issue with my project, which is a 2D platform game where the player controls two characters in the same enviroment using the same controls (if one character moves, the other moves too).

I have two cubes on my scene. Each cube must move and maintain the distance between each other in the X axis. To do this, I modified a script that I find on the Unity forum. I attached the script on both cubes (one named Aubert and other named Jan). Basically, all that the script do is prevent that one cube moves while the other is blocked by some object. I use the character controller to manage the collisions.

My issue is when the player jump and one of the cubes collides with an object, they move in the X axis from different distances, they lose the sync. I think that is because the character controller (I already tried change the character controller settings, with no results).

Here comes the code. "JogadorAntigo" is the name of script that I attached on the cubes.

var speed = 6.0; var jumpSpeed = 8.0; var gravity = 20.0;

private var moveDirection = Vector3.zero; private var grounded : boolean = false; private var player; private var collider2;

//debug private var debug; private var debug2;

function Start() { //on begin, each script points to the other if (gameObject.name == "Aubert") { player = gameObject.Find("Jan"); } if (gameObject.name == "Jan") { player = gameObject.Find("Aubert"); }

collider2 = player.GetComponent(CharacterController);

} function FixedUpdate() {

if (gameObject.name == "Aubert") { //debug debug = gameObject.Find("debug1"); debug2 = gameObject.Find("debug2");

   debug.guiText.text = "Vetor Forcas Jan:" + player.GetComponent("jogadorAntigo").moveDirection;
   debug2.guiText.text = "Vetor Forcas Aubert: " + moveDirection;

}

if (!grounded) { moveDirection.x = Input.GetAxis("Horizontal")*speed; } else { moveDirection = Vector3(Input.GetAxis("Horizontal")*speed, 0.0, 0.0); if (Input.GetButton ("Jump")) { moveDirection.y = jumpSpeed; } } moveDirection = transform.TransformDirection(moveDirection); if(collider2.collisionFlags == 5 || (collider2.collisionFlags & CollisionFlags.Sides)) { moveDirection.x = 0; player.GetComponent("jogadorAntigo").moveDirection.x = 0; }

// Apply gravity moveDirection.y -= gravity * Time.deltaTime;

// Move the controller grounded = (GetComponent(CharacterController).Move(moveDirection * Time.deltaTime) & CollisionFlags.CollidedBelow) != 0;

}

@script RequireComponent(CharacterController)

Any way to sync the two objects (that I'll replace for models later) will be helpful.

I hope you guys forgive my poor english...

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by StephanK · Jun 23, 2010 at 11:41 PM

If you attach this script to both of your characters and one of them collides with an object it won't move in the direction of the collision, but the other one will. If you want to keep the characters at a fixed x-offset you could just set transform.position.x of one character to otherCharacter.transform.position.x - offset. but this may look odd if one character collides with something and the other just stops moving with no obstacle visible.

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

No one has followed this question yet.

Related Questions

General Unity 2D Character/Environment Control Questions 2 Answers

Animated platform and twitching player 0 Answers

CharacterController and moving platform issues 1 Answer

How can i update an objects direction of gravity based on its local rotation? 0 Answers

making "one-way" platform 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