- Home /
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.
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 }
.
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"
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.
Script error: OnTriggerEnter This message parameter has to be of type: Collider The message will be ignored.
@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...
oops, sorry about that, but you should just edit it a little and get what you wanted in the end anyway
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
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);
}
}
}
}
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.
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!
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
Follow this Question
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