Some questions about player movement and teleportation

Hi

I wish make some simples changes to some lines of my code but because I know nothing about C # I can’t. :frowning:
So I ask help here.

Here is my script for player:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Script_player : MonoBehaviour {

public Text AfficheInfoPerso;

// Use this for initialization
void Start() {

}

// Update is called once per frame
void Update()
{

Vector3 newpos = transform.position;
newpos.x += Input.GetAxis(“Horizontal”) / 20;
if (newpos.x < -6.5f) newpos.x = -6.5f;
if (newpos.x > 11.7f) newpos.x = 11.7f;
transform.position = newpos;

}
}

(Sorry I don’t know how insert code in forum. If someone can tell me how, that would be great!).

I wish my player moove automatically in the axe X, in the right direction. And when I press space, he change his direction to go at the left.
So I suppose the line to change it’s “newpos.x += Input.GetAxis(“Horizontal”) / 20;” to have newpos.x which increase alone. But I don’t know what write for do that.
I suppose for space button, I need to create a condition (move_at_left for exemple) and if it’s true, newpos.x decrease. But same, I don’t know what write.

2e point. This is my script for magic portal which teleport player:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Teleporteur_2 : MonoBehaviour {

public Transform Other;

private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == “Player”)
{
collision.transform.position = Other.position;
}
}

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
int r = Random.Range(0, 100);
if (r == 1 && gameObject.activeInHierarchy)
{
gameObject.SetActive(false);
Invoke(“Reappear”, 2.0f);
}

}
void Reappear()
{
gameObject.SetActive(true);
}
}

The second part was give to me by a nice guy in this forum. Unfortunately that didn’t correspond anymore wich what it’s ask for my project.
Now I wish only the sprite dissapear, and when it do, that the player be teleport at a other location than the “Other.position”.
I try to replace “gameObject.SetActive(false);” by SpriteRenderFalse but Unity dont’t know that.

Last point: I will need a other type of portal, which change the current scene of the game. The player don’t need to be teleport; he is place in every scene of the game. So I just need a change of scene when player tag is collision with game object but again I don’t know how write that.

Thanks to read my message.
Have a nice day.

So why not learn a bit of C#? It’s much more effective to spend a few days learning the basics yourself instead of hoping someone will give you exactly what you need on a forum.

Try some tutorials: https://unity3d.com/learn/tutorials

And here’s how to insert code: https://forum.unity3d.com/threads/using-code-tags-properly.143875/

1 Like

I reserved a book in library for learn C# in my native language but I need to wait few week before I can read it (a other guy alredy borrow it). During this time my project need to continu (it’s a schoolar homework) and that is why I ask questions here. :slight_smile:

Well if you’re doing something this advanced and your school hasn’t taught you how to write C# independently, that’s a really bad school. That’s why I recommended the tutorials, you’ll know basic c# that will let you write code for simple stuff like this.

To get how the space key is pressed AND HELD, use Input.GetKey.

As for teleportation, you can just to transform.position = TargetPosition (where TargetPosition is a Vector3). If it’s another object, you can do transform.position = TargetObject.transform.position (where TargetObject is your target object to teleport to).

That doesn’t help a lot but other people help me to do that. This thread can be mark as close.