Question by
LuckyyyRed · Apr 01, 2021 at 10:20 AM ·
scripting problemmovement scriptroad
How to fix bugg, when i jump road disapears
I have problem when i jump in game road script mess up and delete below me road the road logic that road fallows player and destroy segments behind him and add infront but when i added touch support for playing and everything works until i jump heres script for movement end for road logic
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float movmet_s = 10f;
public SpawnHandler sh;
public Joystick joy;
Rigidbody rb;
bool isGrounded = true;
protected float hMovement;
protected float vMovement;
private float ScreenWidth;
public float jumpForce = 2.0f;
Animator anim;
// Start is called before the first frame update
void Start()
{
ScreenWidth = Screen.width;
rb = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
Movement();
}
private void FixedUpdate()
{
}
private void OnTriggerEnter(Collider other)
{
sh.SpawnOnTrigger();
}
private void OnCollisionStay(Collision collision)
{
if(collision.gameObject.tag == "PUT")
{
// isGrounded = true;
}
}
private void Movement()
{
hMovement = joy.Horizontal * (movmet_s / 2);
vMovement = joy.Vertical * movmet_s;
transform.Translate(new Vector3(hMovement, 0, vMovement) * Time.deltaTime);
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Touch touch02 = Input.GetTouch(1);
if (touch02.position.x > ScreenWidth / 2 /*&& isGrounded == true*/)
{
isGrounded = false;
rb.AddForce(0,jumpForce,0,ForceMode.Impulse);
}
}
}
}
And this is Road logic
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class RoadHandler : MonoBehaviour
{
public List<GameObject> roads;
[SerializeField]private float offset = 35f;
private void Start()
{
if(roads != null && roads.Count > 0)
{
roads = roads.OrderBy(r => r.transform.position.x).ToList();
}
}
private void Update()
{
}
public void MoveRoad()
{
GameObject moveRoads = roads[0];
roads.Remove(moveRoads);
float newX = roads[roads.Count - 1].transform.position.x + offset;
moveRoads.transform.position = new Vector3(newX, 0, 0);
roads.Add(moveRoads);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613031552im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
My 2d movement script isn't working 0 Answers
fly movement 0 Answers
Fix Player Movement Script,Fix Player Movement 0 Answers
why does my player move when anywhere on screen is touched? 1 Answer
Movement code in C# works on wrong axis. 0 Answers