i have problem i have runner game when i put another game object or move position y of existing object it changes fwdspeed i want to make it constant
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public enum SIDE {Left, Mid, Right}
public class playermove : MonoBehaviour
{
public SIDE m_side = SIDE.Mid;
float NewXpos = 0f;
[HideInInspector]
public bool SwipeLeft, SwipeRight;
public bool Swipeup, Swipedown;
public float JumpPower = 7f;
private float y;
public float XValue;
public bool Injump;
public bool InRoll;
private CharacterController m_char;
private float x;
public float speeddodge;
private float ColHight;
private float ColCenterY;
//forward speed that's what changing if i add another 3D object value it's the same but speed is different
float FwdSpeed = 0.8f;
void Start()
{
m_char = GetComponent<CharacterController>();
ColHight = m_char.height;
ColCenterY = m_char.center.y;
transform.position = Vector3.zero;
}
void Update()
{
SwipeLeft = Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow);
SwipeRight = Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow);
Swipeup = Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow);
Swipedown = Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow);
if (SwipeLeft)
{
if(m_side == SIDE.Mid)
{
NewXpos = -XValue;
m_side = SIDE.Left;
}else if(m_side == SIDE.Right)
{
NewXpos = 0;
m_side = SIDE.Mid;
}
}
else if (SwipeLeft)
{
if (m_side == SIDE.Left)
{
NewXpos = -XValue + -2;
m_side = SIDE.M_Left;
}
else if (m_side == SIDE.Left)
{
NewXpos = -XValue;
m_side = SIDE.Left;
}
}
else if (SwipeRight)
{
if (m_side == SIDE.Mid)
{
NewXpos = XValue;
m_side = SIDE.Right;
}
else if (m_side == SIDE.Left)
{
NewXpos = 0;
m_side = SIDE.Mid;
}
}
x = Mathf.Lerp(x, NewXpos, Time.deltaTime * speeddodge);
Vector3 MoveVector = new Vector3(x - transform.position.x, y*Time.deltaTime, FwdSpeed);
m_char.Move(MoveVector);
Jump();
Roll();
}
public void Jump()
{
if (m_char.isGrounded)
{
Injump = false;
if (Swipeup)
{
y = JumpPower;
Injump = true;
}
}
else
{
y -= JumpPower * 2 * Time.deltaTime;
if (m_char.velocity.y < 2f)
{
}
}
}
internal float RollCounter;
public void Roll()
{
RollCounter -= Time.deltaTime;
if(RollCounter <= 0)
{
RollCounter = 0f;
m_char.center = new Vector3(0, ColCenterY, 0);
m_char.height = ColHight;
InRoll = false;
}
if (Swipedown)
{
RollCounter = 0.2f;
y -= 10f;
m_char.center = new Vector3(0, ColCenterY/2f, 0);
m_char.height = ColHight/2f;
InRoll = true;
Injump = false;
}
}
}