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 matta9001 · Jan 21, 2014 at 01:19 AM · javascriptnoobladder

How to make a simple ladder?

This is the script i made without really thinking. I've never actually made a script on my own without anybodys help but i want to be able to. So this is the script i made randomly to show the basic idea of what i want to happen var canClimb : false;

 function Update () {
  if (canClimb = true) && (Input.GetButtonDown(KeyCode.W)) {
      transform.Translate (Vector3(0,0,1) * Time.deltaTime*speed);
  }
  if (canClimb = true) && (Input.GetButtonDown(KeyCode.S)) {
      transform.Translate (Vector3(0,0,-1) * Time.deltaTime*speed);
 }
 function OnTriggerEnter () {
     canClimb = true;
 }
 function OnTriggerExit () {
     canClimb = false;
 }

Okay, so i want it so that when a player enters a collider the letter w makes you go up and the letter s makes you go down. What i need to do is make it actually work. Like i know this would never work because unity doesnt know what to move up, but i dont know how to tell it what to move up. I know you can use tags but i dont know how to do that. So tell me if this script is crap and useless, or help me fix it mabye? I just want to have a ladder basically. Its first person so no need for animation.

Comment
Add comment · Show 1
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 Benproductions1 · Jan 21, 2014 at 02:15 AM 0
Share

Please post more specific questions. Not just "$$anonymous$$y script doesn't work, fix it for me". Post the errors you are getting. For one it shouldn't even compile because you are missing a }.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by awesomeface · Jan 21, 2014 at 02:57 AM

I'm going to assume that the script is attached to the ladder, if so, this is what you use

 var playerObject : GameObject;
 var canClimb = false;
 var speed : float = 1;
 
 function Start () {
     playerObject = gameObject.Find("Player");
 }
 
 function OnCollisionEnter (coll : Collision){
     if(coll.gameObject == playerObject){
         canClimb = true;
     }
 }
 
 function OnCollisionExit (coll2 : Collision){
     if(coll2.gameObject == playerObject){
         canClimb = false;
     }
 }
 function Update () {
     if(canClimb){
         if(Input.GetKey(KeyCode.W)){
             playerObject.transform.Translate (Vector3(0,1,0) * Time.deltaTime*speed);
         }
         if(Input.GetKey(KeyCode.S)){
             playerObject.transform.Translate (Vector3(0,-1,0) * Time.deltaTime*speed);
         }
     }
 }

the above script doesn't find an object with the tag "Player", but rather finds the game object called "Player"

Comment
Add comment · Show 14 · 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 matta9001 · Jan 21, 2014 at 04:16 AM 0
Share

Yeah it doesn't exactly work. I named my player "Player". I added a semicolon on line 18. And I dragged player onto the box on the script. It does't work.

avatar image matta9001 · Jan 21, 2014 at 04:17 AM 0
Share

Script error: OnTriggerEnter This message parameter has to be of type: Collider The message will be ignored.

avatar image Benproductions1 · Jan 21, 2014 at 09:18 AM 0
Share

@awesomeface, please post code that actually works and is indented properly. Also, if you assume that the script is attached to the ladder, then you are moving the ladder, not the player...

avatar image awesomeface · Jan 22, 2014 at 05:19 AM 0
Share

oops, sorry about that, but you should just edit it a little and get what you wanted in the end anyway

avatar image awesomeface · Feb 02, 2014 at 10:01 AM 1
Share

You're welcome, from now on I will indent all of my answers, also, matta9001, if my answer is correct, could you please approve it as the correct answer

Show more comments
avatar image
1

Answer by KreeperGaming · Oct 22, 2017 at 08:54 PM

@awesomeface's answer in C#, and doesn't require gameobject's name to be playerObject. Just need tag to be "Player", and it should be that anyways :p

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Ladder : MonoBehaviour {
 
     GameObject playerOBJ;
     bool canClimb = false;
     float speed = 1;
 
     void OnCollisionEnter(Collision coll)
     {
         if (coll.gameObject.tag == "Player")
         {
             canClimb = true;
             playerOBJ = coll.gameObject;
         }
     }
 
     void OnCollisionExit(Collision coll2)
     {
         if (coll2.gameObject.tag == "Player")
         {
             canClimb = false;
             playerOBJ = null;
         }
     }
     void Update()
     {
         if (canClimb)
         {
             if (Input.GetKey(KeyCode.W))
             {
                 playerOBJ.transform.Translate(new Vector3(0, 1, 0) * Time.deltaTime * speed);
             }
             if (Input.GetKey(KeyCode.S))
             {
                 playerOBJ.transform.Translate(new Vector3(0, -1, 0) * Time.deltaTime * speed);
             }
         }
     }
 }
 
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 DawPiot14 · Jun 22, 2020 at 02:53 PM 0
Share

Hi, I know this is old, but can you please reply. Do I put this script on the player or on the ladder? I tried it on the ladder and it didn't work.

avatar image
-1

Answer by GraviterX · Jan 21, 2014 at 02:39 AM

Hi, try using animations with a trigger so something like:

 function OnTriggerEnter() {
     animation.Play();
 }

Hope this helps!

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 Benproductions1 · Jan 21, 2014 at 09:21 AM 0
Share

The question asked specifically about user input controlling the position of the player in the sense of ladders, not animations. This is a valid way of making ladders, but not what was asked in the question.

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

25 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

Related Questions

Help a beginner with a small doubt ? 2 Answers

Health Script Help...? 1 Answer

Help with OnCollision 2 Answers

can anyone help me stop a rigidbody from rotating around its z axis 1 Answer

Adding an offset to an instantiated objects rotation 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