Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Golem_Leader · Sep 07, 2015 at 04:11 PM · c#animationcharacter

I have my animations created. Now what?

Being relatively new to Unity, I have many problems with creating. I got my animation all set up (Strike, Idle, Jump, Run, Fall) but I can't figure out a few things. I already have a script set up for the player, if that helps at all.

  1. Now that that's done, how do I implement it to the character?

  2. Is there a way to do it without my character falling apart at the launch of the game?

  3. Am I able to implement the animations without rigid body?

  4. The other basic stuff I should know

I'm running it with C#, and don't know how to convert to javascript, so I ask that you don't respond with javascript. Code that goes on my Player:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (Controller2D))]
 public class Player : MonoBehaviour {
     
     public float maxJumpHeight = 4;
     public float minJumpHeight = 1;
     public float timeToJumpApex = .4f;
     float accelerationTimeAirborne = .2f;
     float accelerationTimeGrounded = .1f;
     float moveSpeed = 6;
     
     public Vector2 wallJumpClimb;
     public Vector2 wallJumpOff;
     public Vector2 wallLeap;
     
     public float wallSlideSpeedMax = 3;
     public float wallStickTime = .25f;
     float timeToWallUnstick;
     
     float gravity;
     float maxJumpVelocity;
     float minJumpVelocity;
     Vector3 velocity;
     float velocityXSmoothing;
     
     Controller2D controller;
     
     void Start() {
         controller = GetComponent<Controller2D> ();
         
         gravity = -(2 * maxJumpHeight) / Mathf.Pow (timeToJumpApex, 2);
         maxJumpVelocity = Mathf.Abs(gravity) * timeToJumpApex;
         minJumpVelocity = Mathf.Sqrt (2 * Mathf.Abs (gravity) * minJumpHeight);
         print ("Gravity: " + gravity + "  Jump Velocity: " + maxJumpVelocity);
     }
     
     void Update() {
         Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
         int wallDirX = (controller.collisions.left) ? -1 : 1;
         
         float targetVelocityX = input.x * moveSpeed;
         velocity.x = Mathf.SmoothDamp (velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below)?accelerationTimeGrounded:accelerationTimeAirborne);
         
         bool wallSliding = false;
         if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) {
             wallSliding = true;
             
             if (velocity.y < -wallSlideSpeedMax) {
                 velocity.y = -wallSlideSpeedMax;
             }
             
             if (timeToWallUnstick > 0) {
                 velocityXSmoothing = 0;
                 velocity.x = 0;
                 
                 if (input.x != wallDirX && input.x != 0) {
                     timeToWallUnstick -= Time.deltaTime;
                 }
                 else {
                     timeToWallUnstick = wallStickTime;
                 }
             }
             else {
                 timeToWallUnstick = wallStickTime;
             }
             
         }
         
         if (Input.GetKeyDown (KeyCode.Space)) {
             if (wallSliding) {
                 if (wallDirX == input.x) {
                     velocity.x = -wallDirX * wallJumpClimb.x;
                     velocity.y = wallJumpClimb.y;
                 }
                 else if (input.x == 0) {
                     velocity.x = -wallDirX * wallJumpOff.x;
                     velocity.y = wallJumpOff.y;
                 }
                 else {
                     velocity.x = -wallDirX * wallLeap.x;
                     velocity.y = wallLeap.y;
                 }
             }
             if (controller.collisions.below) {
                 velocity.y = maxJumpVelocity;
             }
         }
         if (Input.GetKeyUp (KeyCode.Space)) {
             if (velocity.y > minJumpVelocity) {
                 velocity.y = minJumpVelocity;
             }
         }
         
         
         velocity.y += gravity * Time.deltaTime;
         controller.Move (velocity * Time.deltaTime, input);
         
         if (controller.collisions.above || controller.collisions.below) {
             velocity.y = 0;
         }
         
     }
 }


Code that goes with it:

 using UnityEngine;
 using System.Collections;
 
 public class Controller2D : RaycastController {
     
     float maxClimbAngle = 80;
     float maxDescendAngle = 80;
     
     public CollisionInfo collisions;
     [HideInInspector]
     public Vector2 playerInput;
     
     public override void Start() {
         base.Start ();
         collisions.faceDir = 1;
         
     }
     
     public void Move(Vector3 velocity, bool standingOnPlatform) {
         Move (velocity, Vector2.zero, standingOnPlatform);
     }
     
     public void Move(Vector3 velocity, Vector2 input, bool standingOnPlatform = false) {
         UpdateRaycastOrigins ();
         collisions.Reset ();
         collisions.velocityOld = velocity;
         playerInput = input;
         
         if (velocity.x != 0) {
             collisions.faceDir = (int)Mathf.Sign(velocity.x);
         }
         
         if (velocity.y < 0) {
             DescendSlope(ref velocity);
         }
         
         HorizontalCollisions (ref velocity);
         if (velocity.y != 0) {
             VerticalCollisions (ref velocity);
         }
         
         transform.Translate (velocity);
         
         if (standingOnPlatform) {
             collisions.below = true;
         }
     }
     
     void HorizontalCollisions(ref Vector3 velocity) {
         float directionX = collisions.faceDir;
         float rayLength = Mathf.Abs (velocity.x) + skinWidth;
         
         if (Mathf.Abs(velocity.x) < skinWidth) {
             rayLength = 2*skinWidth;
         }
         
         for (int i = 0; i < horizontalRayCount; i ++) {
             Vector2 rayOrigin = (directionX == -1)?raycastOrigins.bottomLeft:raycastOrigins.bottomRight;
             rayOrigin += Vector2.up * (horizontalRaySpacing * i);
             RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, collisionMask);
             
             Debug.DrawRay(rayOrigin, Vector2.right * directionX * rayLength,Color.red);
             
             if (hit) {
                 
                 if (hit.distance == 0) {
                     continue;
                 }
                 
                 float slopeAngle = Vector2.Angle(hit.normal, Vector2.up);
                 
                 if (i == 0 && slopeAngle <= maxClimbAngle) {
                     if (collisions.descendingSlope) {
                         collisions.descendingSlope = false;
                         velocity = collisions.velocityOld;
                     }
                     float distanceToSlopeStart = 0;
                     if (slopeAngle != collisions.slopeAngleOld) {
                         distanceToSlopeStart = hit.distance-skinWidth;
                         velocity.x -= distanceToSlopeStart * directionX;
                     }
                     ClimbSlope(ref velocity, slopeAngle);
                     velocity.x += distanceToSlopeStart * directionX;
                 }
                 
                 if (!collisions.climbingSlope || slopeAngle > maxClimbAngle) {
                     velocity.x = (hit.distance - skinWidth) * directionX;
                     rayLength = hit.distance;
                     
                     if (collisions.climbingSlope) {
                         velocity.y = Mathf.Tan(collisions.slopeAngle * Mathf.Deg2Rad) * Mathf.Abs(velocity.x);
                     }
                     
                     collisions.left = directionX == -1;
                     collisions.right = directionX == 1;
                 }
             }
         }
     }
     
     void VerticalCollisions(ref Vector3 velocity) {
         float directionY = Mathf.Sign (velocity.y);
         float rayLength = Mathf.Abs (velocity.y) + skinWidth;
         
         for (int i = 0; i < verticalRayCount; i ++) {
             
             Vector2 rayOrigin = (directionY == -1)?raycastOrigins.bottomLeft:raycastOrigins.topLeft;
             rayOrigin += Vector2.right * (verticalRaySpacing * i + velocity.x);
             RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collisionMask);
             
             Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength,Color.red);
             
             if (hit) {
                 if (hit.collider.tag == "Through") {
                     if (directionY == 1 || hit.distance == 0) {
                         continue;
                     }
                     if (collisions.fallingThroughPlatform) {
                         continue;
                     }
                     if (playerInput.y == -1) {
                         collisions.fallingThroughPlatform = true;
                         Invoke("ResetFallingThroughPlatform",.5f);
                         continue;
                     }
                 }
                 
                 velocity.y = (hit.distance - skinWidth) * directionY;
                 rayLength = hit.distance;
                 
                 if (collisions.climbingSlope) {
                     velocity.x = velocity.y / Mathf.Tan(collisions.slopeAngle * Mathf.Deg2Rad) * Mathf.Sign(velocity.x);
                 }
                 
                 collisions.below = directionY == -1;
                 collisions.above = directionY == 1;
             }
         }
         
         if (collisions.climbingSlope) {
             float directionX = Mathf.Sign(velocity.x);
             rayLength = Mathf.Abs(velocity.x) + skinWidth;
             Vector2 rayOrigin = ((directionX == -1)?raycastOrigins.bottomLeft:raycastOrigins.bottomRight) + Vector2.up * velocity.y;
             RaycastHit2D hit = Physics2D.Raycast(rayOrigin,Vector2.right * directionX,rayLength,collisionMask);
             
             if (hit) {
                 float slopeAngle = Vector2.Angle(hit.normal,Vector2.up);
                 if (slopeAngle != collisions.slopeAngle) {
                     velocity.x = (hit.distance - skinWidth) * directionX;
                     collisions.slopeAngle = slopeAngle;
                 }
             }
         }
     }
     
     void ClimbSlope(ref Vector3 velocity, float slopeAngle) {
         float moveDistance = Mathf.Abs (velocity.x);
         float climbVelocityY = Mathf.Sin (slopeAngle * Mathf.Deg2Rad) * moveDistance;
         
         if (velocity.y <= climbVelocityY) {
             velocity.y = climbVelocityY;
             velocity.x = Mathf.Cos (slopeAngle * Mathf.Deg2Rad) * moveDistance * Mathf.Sign (velocity.x);
             collisions.below = true;
             collisions.climbingSlope = true;
             collisions.slopeAngle = slopeAngle;
         }
     }
     
     void DescendSlope(ref Vector3 velocity) {
         float directionX = Mathf.Sign (velocity.x);
         Vector2 rayOrigin = (directionX == -1) ? raycastOrigins.bottomRight : raycastOrigins.bottomLeft;
         RaycastHit2D hit = Physics2D.Raycast (rayOrigin, -Vector2.up, Mathf.Infinity, collisionMask);
         
         if (hit) {
             float slopeAngle = Vector2.Angle(hit.normal, Vector2.up);
             if (slopeAngle != 0 && slopeAngle <= maxDescendAngle) {
                 if (Mathf.Sign(hit.normal.x) == directionX) {
                     if (hit.distance - skinWidth <= Mathf.Tan(slopeAngle * Mathf.Deg2Rad) * Mathf.Abs(velocity.x)) {
                         float moveDistance = Mathf.Abs(velocity.x);
                         float descendVelocityY = Mathf.Sin (slopeAngle * Mathf.Deg2Rad) * moveDistance;
                         velocity.x = Mathf.Cos (slopeAngle * Mathf.Deg2Rad) * moveDistance * Mathf.Sign (velocity.x);
                         velocity.y -= descendVelocityY;
                         
                         collisions.slopeAngle = slopeAngle;
                         collisions.descendingSlope = true;
                         collisions.below = true;
                     }
                 }
             }
         }
     }
     
     void ResetFallingThroughPlatform() {
         collisions.fallingThroughPlatform = false;
     }
     
     public struct CollisionInfo {
         public bool above, below;
         public bool left, right;
         
         public bool climbingSlope;
         public bool descendingSlope;
         public float slopeAngle, slopeAngleOld;
         public Vector3 velocityOld;
         public int faceDir;
         public bool fallingThroughPlatform;
         
         public void Reset() {
             above = below = false;
             left = right = false;
             climbingSlope = false;
             descendingSlope = false;
             
             slopeAngleOld = slopeAngle;
             slopeAngle = 0;
         }
     }
     
 }


I realize this is a lot, but if I can get some help it will very much be appreciated.

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

2 People are following this question.

avatar image avatar image

Related Questions

Setup an animation script, but now mouselook is not working? 0 Answers

Looking to activate animation when clicking a button 0 Answers

Rigid body robot animation 0 Answers

Help with a door slam trap 0 Answers

How can I edit animations of a character downloaded from Asset Store? 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