• 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 cookywooky · Sep 01, 2020 at 04:26 PM · colliderstriggersrigidbody.addforcetriggering animation box collider

Some questions about a jump pad (2D)

Hi so I'm a definite newbie and I'm still figuring out a lot of the basics of coding, C# and Unity. I've been working on a little 2D platformer project and I'm trying to code a kind of launchpad, which the player will walk over and will then shoot them into the air, as well as play the animation. The launcher is in the form of a water spout.


There are a few problems:

  1. The player jump animation doesn't trigger when stepping on the launchpad. Is this something I should code into the actual player animation script?

  2. The launchpad works perfectly if you run up to it, but if you jump on it, it either doesn't trigger at all or works differently (like 3 small jumps, followed up a proper jump)

  3. The last thing is that I need to drag my character controller for my player to the right space in the inspector window for the launchpad each time I run the scene. I have it set as such before-hand, but it always gets set to "none" when I run it, and then I have to do it manually. I was told that this is because I have player = GetComponent() placed in Start(), although I'm not sure how to code this without doing that.

Any help/tips would be much appreciated.

(copied from another board because it seems to appropriate here.)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 public class Spout : MonoBehaviour
 {
     public Animator animator;
     public float spoutThrust = 20f;
     public CharacterController2D player;
     private PlayerMoveScript playerMove;
  
 void Start()
 {
     player = GetComponent<CharacterController2D>();
     playerMove = GetComponent<PlayerMoveScript>();
 }
  
 private void OnTriggerEnter2D(Collider2D collider)
 {
     Debug.Log("Triggered the spout!");
     animator.SetBool("PlayerContact", true);
     if (collider.tag == "Player")
     {
             player.m_Grounded = false;
             player.m_Rigidbody2D.AddForce(new Vector2(0f, player.m_JumpForce * spoutThrust));
             playerMove.jump = true;
             playerMove.animator.SetBool("IsJumping", true);
     }
 }
  
 private void OnTriggerExit2D(Collider2D collider)
 {
     Debug.Log("The spout stopped");
     animator.SetBool("PlayerContact", false);
 }
  
 } //end



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

Answer by Chrissyeah_ · Sep 01, 2020 at 10:31 PM

@cookywooky

  1. Yes

  2. You are using AddForce, which is a sum applied to your existing velocity. If you have an existing downward velocity, the force you add on the opposing direction will first need to account for the negative force. A solution I can suggest is setting the velocity instead, preserving the velocity on the X axis. player.m_Rigidbody2D.velocity = new Vector2(player.m_Rigidbody2D.velocity.x, player.m_JumpForce * spoutThrust);

  3. in the start method you say this player = GetComponent<CharacterController2D>(); This means that you are getting the characterController2D from the exact gameobject this script is also attached to. If it is not, it will return none. So if you want to drag it in using the inspector, you will need to remove the code from your start method in order to keep the inspector settings. It's important to understand when dragging things into the script using the inspector is a good idea. For instance you can not drag something from the scene into a prefab in your assets.

Comment
Add comment · Show 1 · 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 cookywooky · Sep 02, 2020 at 03:17 PM 0
Share

Thanks for the answer, it helps a lot!

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

212 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 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 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 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 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 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 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 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 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 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 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 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

Pipe Game ... I have to trace water from source to destination 2 Answers

Calling "OnTriggerEnter" when a parent object has a rigidbody 0 Answers

Problem With Triggers? 0 Answers

When Near a door, Press a button to go to the other side. 1 Answer

Need help with using Destroy with a variable condition 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