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
1
Question by franktinsley · Jul 13, 2010 at 05:23 AM · collisionphysicsiphonecharactercontrollerplatform

Trying to avoid collision with sides and interiors of polygons

Hi everyone! I'm working on a very simple game to get myself familiar with Unity iPhone and I've got almost all the major parts working great but I was wondering if anyone can help me out with one little problem. I've got a player character that's setup to move forward, backward, and sideways by tilting the iPhone and he jumps the moment he's on the ground, over and over. This is actually what I want for the gameplay. My only trick now is getting my player character to correctly move through platforms so he can jump on top of them but have no trouble passing right through them from underneath. I'm using flat polygon meshes for collision so he's able to make it through but he seems to collide with them from the sides and their interiors making movement a bit clunky when he's jumping up through groups of small platforms. Here's my code for the character controller, kind of a stripped down hack job of the Player Relative Control script included with Unity iPhone:

@script RequireComponent( CharacterController )

// This script must be attached to a GameObject that has a CharacterController

var forwardSpeed : float = 4; var backwardSpeed : float = 1; var sidestepSpeed : float = 1; var jumpSpeed : float = 8;

private var thisTransform : Transform; private var character : CharacterController; private var velocity : Vector3; // Used for continuing momentum while in air

function Start() { // Cache component lookup at startup instead of doing this every frame
thisTransform = GetComponent( Transform ); character = GetComponent( CharacterController );

 // Move the character to the correct start position in the level, if one exists
 var spawn = GameObject.Find( "PlayerSpawn" );
 if ( spawn )
     thisTransform.position = spawn.transform.position;

}

function Update() { var dir : Vector3 = Vector3.zero;

 dir.x = -iPhoneInput.acceleration.y;
 dir.z = iPhoneInput.acceleration.x;


 var movement = thisTransform.TransformDirection( Vector3( dir.x, 0, dir.z ) );

 // We only want horizontal movement
 movement.y = 0;
 movement.Normalize();

 // Apply movement from move joystick
 var absJoyPos = Vector2( Mathf.Abs( dir.x ), Mathf.Abs( dir.z ) );  
 if ( absJoyPos.y > absJoyPos.x )
 {
     if ( dir.z > 0 )
         movement *= forwardSpeed * absJoyPos.y;
     else
     {
         movement *= backwardSpeed * absJoyPos.y;
     }
 }
 else
 {
     movement *= sidestepSpeed * absJoyPos.x;
 }

 // Check for jump
 if ( character.isGrounded )
 {
     // Apply the current movement to launch velocity        

// velocity = character.velocity; velocity.y = jumpSpeed;
} else {
// Apply gravity to our velocity to diminish it over time velocity.y += Physics.gravity.y * Time.deltaTime; }

 movement += velocity;   
 movement += Physics.gravity;
 movement *= Time.deltaTime;

 // Actually move the character  
 character.Move( movement );

}

Thanks looking and any ideas someone might have! I am LOVING this Unity stuff SO MUCH! Seriously best development community I have EVER SEEN!

Oh and any ideas on simplifying my code for tilting to move and adjusting the starting angle the iPhone is held at would be AMAZING but no worries on that, I'll find ways eventually. XD

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
Best Answer

Answer by Noise crime · Jul 13, 2010 at 06:06 AM

I would probably go for a simple raycast from the players feet level downwards, but only when the players velocity is downwards. That way you should get accurate collision detection with the platforms only when you can 'land' on them and thus allow the player to jump up 'through' them.

You may need to use 2 ray casts one for each edge/foot of the player, so they can land half on a platform.

Alternatively you add a second collider to the player and attach it to around their feet level. Then use that to check for collisions with platforms, but only if the player velocity is downwards. If the velocity is upwards, ignore any collisions. Only issue here is if the player feet collider is 'within' the platform collider as the velocity changes, you might find they get stuck in the platform.

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

Removing velocity relative to the contact normal 1 Answer

Alternatives to CharacterController.Move to drive a car 1 Answer

How can I make two Character Controllers Collide 2 Answers

Bullets colliding with Character Controller 3 Answers

Massive framerate fall with Character Joint 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