Sorry for posting twice about the same script, but I’m having a different issue this time that is really stumping me. When this code was in my Player script it worked fine and my character would slide as intended. When I moved the code to a script on a child object of the Player, it not longer moves the player. The other effects happen as intended, but the character doesn’t move. There are no errors. Any insights into what I am doing wrong or what I need to change? Do you need more information about my Player Script?
public class abSlide : MonoBehaviourPunCallbacks
{
public float cooldown;
public float lengthOfSlide;
public float slideModifier;
public float slideFOVModifier;
public float slideCameraDropModifier;
public Player player;
public Weapon weapon;
public bool sprintActivatedSliding;
public bool JumpWhenSliding;
public bool ShootWhenSliding;
public KeyCode ability1;
private bool isCooldown = false;
private bool sliding;
private bool isSliding;
private float slide_time;
private Vector3 slide_direction;
private Image ui_ability1;
private void Start()
{
if (photonView.IsMine)
{
ui_ability1 = GameObject.Find("HUD/Ability1/Icon").GetComponent<Image>();
ui_ability1.fillAmount = 1;
}
}
private void FixedUpdate()
{
if (!photonView.IsMine) return;
if (sprintActivatedSliding)
{
//Controls
bool slide = Input.GetKey(KeyCode.LeftControl);
//States
isSliding = player.isSprinting && slide && !sliding && isCooldown == false;
}
else
{
//Controls
bool slide = Input.GetKey(ability1);
//States
isSliding = slide && !sliding && isCooldown == false;
}
if (sliding)
{
player.direction = slide_direction;
player.adjustedSpeed *= slideModifier;
slide_time -= Time.deltaTime;
if (slide_time <= 0)
{
sliding = false;
player.alternativeMovement = false;
player.weaponParentCurrentPosition += Vector3.up * slideCameraDropModifier;
}
}
//Sliding
if (isSliding)
{
player.alternativeMovement = true;
sliding = true;
slide_direction = player.direction;
slide_time = lengthOfSlide;
player.weaponParentCurrentPosition += Vector3.down * slideCameraDropModifier;
isCooldown = true;
ui_ability1.fillAmount = 0;
}
if (sliding)
{
player.normalCam.fieldOfView = Mathf.Lerp(player.normalCam.fieldOfView, player.baseFOV * slideFOVModifier, Time.deltaTime * 8f);
player.normalCam.transform.localPosition = Vector3.Lerp(player.normalCam.transform.localPosition, player.origin + Vector3.down * slideCameraDropModifier, Time.deltaTime * 6f);
}
//Cooldown/Timer
if (isCooldown)
{
ui_ability1.fillAmount += 1 / cooldown * Time.deltaTime;
if (ui_ability1.fillAmount >= 1)
{
ui_ability1.fillAmount = 1;
isCooldown = false;
}
}
}
}
You have three very similar variables: slide, sliding and isSliding. I would rename them (or combine them) to avoid confusion and hopefully make it easier to figure out the issue.
Could you post your Player class? The code above doesn’t seem to actually move the player.
Ok, here is my abSlide scripts with the variable names updated.
public class abSlide : MonoBehaviourPunCallbacks
{
public float cooldown;
public float lengthOfSlide;
public float slideModifier;
public float slideFOVModifier;
public float slideCameraDropModifier;
public Player player;
public Weapon weapon;
public bool sprintActivatedSliding;
public bool JumpWhenSliding; //not doing anything right now
public bool ShootWhenSliding; //not doing anything right now
public KeyCode ability1;
private bool isCooldown = false;
private bool sliding;
private bool startAbility;
private float slide_time;
private Vector3 slide_direction;
private Image ui_ability1;
private void Start()
{
if (photonView.IsMine)
{
ui_ability1 = GameObject.Find("HUD/Ability1/Icon").GetComponent<Image>();
ui_ability1.fillAmount = 1;
}
}
private void FixedUpdate()
{
if (!photonView.IsMine) return;
if (sprintActivatedSliding)
{
//Controls
bool activate = Input.GetKey(KeyCode.LeftControl);
//States
startAbility = player.isSprinting && activate && !sliding && isCooldown == false;
}
else
{
//Controls
bool activate = Input.GetKey(ability1);
//States
startAbility = activate && !sliding && isCooldown == false;
}
if (sliding)
{
player.direction = slide_direction;
player.adjustedSpeed *= slideModifier;
slide_time -= Time.deltaTime;
if (slide_time <= 0)
{
sliding = false;
player.alternativeMovement = false;
player.weaponParentCurrentPosition += Vector3.up * slideCameraDropModifier;
}
}
//Sliding
if (startAbility)
{
player.alternativeMovement = true;
sliding = true;
slide_direction = player.direction;
slide_time = lengthOfSlide;
player.weaponParentCurrentPosition += Vector3.down * slideCameraDropModifier;
isCooldown = true;
ui_ability1.fillAmount = 0;
}
if (sliding)
{
player.normalCam.fieldOfView = Mathf.Lerp(player.normalCam.fieldOfView, player.baseFOV * slideFOVModifier, Time.deltaTime * 8f); //current FOV when sliding equivolent to sprintFOVModifier (1.3) times 1.25
player.normalCam.transform.localPosition = Vector3.Lerp(player.normalCam.transform.localPosition, player.origin + Vector3.down * slideCameraDropModifier, Time.deltaTime * 6f);
}
//Cooldown/Timer
if (isCooldown)
{
ui_ability1.fillAmount += 1 / cooldown * Time.deltaTime;
if (ui_ability1.fillAmount >= 1)
{
ui_ability1.fillAmount = 1;
isCooldown = false;
}
}
}
}
Here is my Player script, though only the bits that are related to movement.
public class Player : MonoBehaviourPunCallbacks
{
#region Variables
public float speed;
public float sprintModifier;
public float jumpForce;
public Transform groundDetector;
public LayerMask ground;
[HideInInspector] public float adjustedSpeed;
[HideInInspector] public Vector3 direction;
[HideInInspector] public bool alternativeMovement;
[HideInInspector] public bool canJump; //not doing anything right now
[HideInInspector] public bool isSprinting;
private Rigidbody rig;
#endregion
#region Monobehavior Callbacks
private void Start()
{
rig = GetComponent<Rigidbody>();
if (photonView.IsMine)
{
alternativeMovement = false;
}
}
private void Update()
{
if (!photonView.IsMine) return;
//Axis
float t_hmove = Input.GetAxisRaw("Horizontal");
float t_vmove = Input.GetAxisRaw("Vertical");
//Controls
bool sprint = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
bool jump = Input.GetKeyDown(KeyCode.Space);
//States
bool isGrounded = Physics.Raycast(groundDetector.position, Vector3.down, 0.2f, ground);
bool isJumping = jump && isGrounded;
isSprinting = sprint && t_vmove > 0 && !isJumping && isGrounded;
//Jumping
if (isJumping)
{
rig.AddForce(Vector3.up * jumpForce);
}
}
void FixedUpdate()
{
if (!photonView.IsMine) return;
//Axis
float t_hmove = Input.GetAxisRaw("Horizontal");
float t_vmove = Input.GetAxisRaw("Vertical");
//Controls
bool sprint = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
bool jump = Input.GetKeyDown(KeyCode.Space);
//States
bool isGrounded = Physics.Raycast(groundDetector.position, Vector3.down, 0.2f, ground);
bool isJumping = jump && isGrounded;
isSprinting = sprint && t_vmove > 0 && !isJumping && isGrounded;
//Movement
direction = Vector3.zero;
adjustedSpeed = speed;
if (!alternativeMovement)
{
direction = new Vector3(t_hmove, 0, t_vmove);
direction.Normalize();
direction = transform.TransformDirection(direction);
if (isSprinting) adjustedSpeed *= sprintModifier;
}
Vector3 t_targetVelocity = direction * adjustedSpeed * Time.deltaTime;
t_targetVelocity.y = rig.velocity.y;
rig.velocity = t_targetVelocity;
}
#endregion
}
You set the velocity as:
Vector3 t_targetVelocity = direction * adjustedSpeed * Time.deltaTime;
adjustedSpeed is defined as:
adjustedSpeed = speed;
But it seems you never write to speed, so speed is always zero, so adjustedSpeed is always zero, so direction * adjustedSpeed * Time.deltaTime is always zero. So your velocity is always zero.
I have speed as a public float. If that is not working what should I do to fix that? Assume I don’t know what I’m really doing.
Oh I see now - you’ve set Speed in the editor.
In that case it must be that your direction Vector is to blame. direction is defined as
direction = new Vector3(t_hmove, 0, t_vmove);
But direction is only initialised if alternativeMovement == false. So make sure alternativeMovement is actually false by doing Debug.Log(“alternativeMovement=”+alternativeMovement);
Perhaps this line is not giving you the results you’re expecting?:
if (photonView.IsMine)
{
alternativeMovement = false;
}
Time.deltaTime is unnecessary in FixedUpdate because FixedUpdate is always called at the same frequency.
Failing that all I can think of to suggest is to make sure that t_hmove and t_vmove do not equal zero when you press the input keys.
Lastly the bug might be here:
if (startAbility)
{
player.alternativeMovement = true;
sliding = true;
slide_direction = player.direction;
slide_time = lengthOfSlide;
player.weaponParentCurrentPosition += Vector3.down * slideCameraDropModifier;
isCooldown = true;
ui_ability1.fillAmount = 0;
}
if startAbility is true, then it will set alternativeMovement to true, which means that your player will never be able to move, because the direction vector is only set if alternativeMovement == false.
Ok, after messing around with Debug for a bit, I think my problem is in this section.
- player.direction = slide_direction;
- player.adjustedSpeed *= slideModifier;
Specifically here, my code seems to be acting one way. This script can pull these values from the Player script, but it doesn’t go other other way and alter those values on the Player script. Any idea why these values aren’t being changed in my Player script?
The reason it doesn’t alter it is because your Player script always overrides them. You have in your code:
direction = Vector3.zero;
adjustedSpeed = speed;
if (!alternativeMovement)
{
direction = new Vector3(t_hmove, 0, t_vmove);
direction.Normalize();
direction = transform.TransformDirection(direction);
if (isSprinting) adjustedSpeed *= sprintModifier;
}
So this means that no matter what your abSlide class does, it always gets overwritten.
Your velocity equation is Vector3 t_targetVelocity = direction * adjustedSpeed * Time.deltaTime. If your player doesn’t move, then it must be the case that either direction, adjustedSpeed or Time.deltaTime is equal to zero. It can’t be Time.deltaTime, because that’s impossibe. It can’t be adjustedSpeed, because adjustedSpeed=speed (which equals 500 in your editor). So it must be that direction = 0, 0, 0 for some reason. Sleep on it and come back to it tomorrow
1 Like
I fixed it! What you were saying about the player script overriding the abSlide script seems to be true, so I moved where I set adjustedSpeed in my player script to be after I set targetVelocity and rig.velocity, and removed the code on setting direction to Vector3.zero.
if (!alternativeMovement)
{ direction = new Vector3(t_hmove, 0, t_vmove);
direction.Normalize();
direction = transform.TransformDirection(direction);
if (isSprinting) adjustedSpeed *= sprintModifier;
}
Vector3 t_targetVelocity = direction * adjustedSpeed * Time.deltaTime;
t_targetVelocity.y = rig.velocity.y;
rig.velocity = t_targetVelocity;
adjustedSpeed = speed;
1 Like