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 16snathan · Feb 03, 2014 at 06:12 AM · collisionplayerplatformmoving

Moving Platform Collisions with Custom Movement Script

I am programming a platformer game and am having issues with moving platform collisions. HOWEVER, I am NOT using CharacterController. I am using a custom script from this tutorial: http://www.youtube.com/watch?annotation_id=annotation_299760&feature=iv&src_vid=d3HEFiDFApI&v=mN8alpNdTUk

and am having issues with moving platform scripts. Does anyone know of any scripts that might help me?

This script controls the player physics: using UnityEngine; using System.Collections;

[RequireComponent (typeof(BoxCollider))] public class PlayerPhysics : MonoBehaviour {

 public LayerMask collisionMask;

 private BoxCollider collider;
 private Vector3 s;
 private Vector3 c;
 
 private float skin = 0.005f; //0.005f
 
 [HideInInspector]
 public bool grounded;
 [HideInInspector]
 public bool movementStopped;
 
 Ray ray;
 RaycastHit hit;
 
 void Start() {
     collider = GetComponent<BoxCollider>();
     s = collider.size;
     c = collider.center;
 }

 public void Move(Vector2 moveAmount) {
     
     float deltaY = moveAmount.y;
     float deltaX = moveAmount.x;
     Vector2 p = transform.position;
     
     // Check collisions above and below
     grounded = false;
     
     for (int i = 0; i<3; i ++) {
         float dir = Mathf.Sign(deltaY);
         float x = (p.x + c.x - s.x/2) + s.x/2 * i; // Left, centre and then rightmost point of collider
         float y = p.y + c.y + s.y/2 * dir; // Bottom of collider
         
         ray = new Ray(new Vector2(x,y), new Vector2(0,dir));
         Debug.DrawRay(ray.origin,ray.direction);
         if (Physics.Raycast(ray,out hit,Mathf.Abs(deltaY) + skin,collisionMask)) {
             // Get Distance between player and ground
             float dst = Vector3.Distance (ray.origin, hit.point);
             
             // Stop player's downwards movement after coming within skin width of a collider
             if (dst > skin) {
                 deltaY = dst * dir - skin * dir;
             }
             else {
                 deltaY = 0;
             }
             
             grounded = true;
             
             break;
             
         }
     }
     
     
             // Check collisions left and right
     
     movementStopped = false;
     
     for (int i = 0; i<3; i ++) {
         float dir = Mathf.Sign(deltaX);
         float x = p.x + c.x + s.x/2 * dir; // Left, centre and then rightmost point of collider
         float y = p.y + c.y - s.y/2 + s.y/2 * i; // Bottom of collider
         
         ray = new Ray(new Vector2(x,y), new Vector2(dir,0));
         Debug.DrawRay(ray.origin,ray.direction);
         if (Physics.Raycast(ray,out hit,Mathf.Abs(deltaX) + skin,collisionMask)) {
             // Get Distance between player and ground
             float dst = Vector3.Distance (ray.origin, hit.point);
             
             // Stop player's downwards movement after coming within skin width of a collider
             if (dst > skin) {
                 deltaX = dst * dir - skin * dir;
             }
             else {
                 deltaX = 0;
             }
             
             movementStopped = true;
             
             break;
             
         }
     }
 
     
     Vector2 finalTransform = new Vector2(deltaX,deltaY);
     
     transform.Translate(finalTransform);
 }
 

}

and this script controls the player's movement: using System.Collections;

[RequireComponent(typeof(PlayerPhysics))] public class PlayerController : MonoBehaviour {

 // Player Handling
 public float gravity = 20;
 public float speed = 8;
 public float acceleration = 30;
 public float jumpHeight = 12;
 
 private float currentSpeed;
 private float targetSpeed;
 private Vector2 amountToMove;
 
 private PlayerPhysics playerPhysics;
 

 void Start () {
     playerPhysics = GetComponent<PlayerPhysics>();
 }
 
 void Update () {
     // Reset acceleration upon collision
     if (playerPhysics.movementStopped) {
         targetSpeed = 0;
         currentSpeed = 0;
     }
     
     // If player is touching the ground
     if (playerPhysics.grounded) {
         amountToMove.y = 0;
         
         // Jump
         if (Input.GetButtonDown("Jump")) {
             amountToMove.y = jumpHeight;    
         }
     }
     
     // Input
     targetSpeed = Input.GetAxisRaw("Horizontal") * speed;
     currentSpeed = IncrementTowards(currentSpeed, targetSpeed,acceleration);
     
     // Set amount to move
     amountToMove.x = currentSpeed;
     amountToMove.y -= gravity * Time.deltaTime;
     playerPhysics.Move(amountToMove * Time.deltaTime);
 }
 
 // Increase n towards target by speed
 private float IncrementTowards(float n, float target, float a) {
     if (n == target) {
         return n;    
     }
     else {
         float dir = Mathf.Sign(target - n); // must n be increased or decreased to get closer to target
         n += a * Time.deltaTime * dir;
         return (dir == Mathf.Sign(target-n))? n: target; // if n has now passed target then return target, otherwise return n
     }
 }

}

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

18 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

Related Questions

Do a collider only with certain taged objects 2 Answers

Endless DeathZone??!!? 4 Answers

Object move without an input 1 Answer

Making the object collide while moving in the forward dir. (Vector3.forward) 1 Answer

Two gameObjects on collision one increase the health and the other decrease the health of the player. 2 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