Hi, guys i have a problem and would appreciate your help
this is an endless runner player mover script
my problem is that when i stick the player to the boundary (press left or right and hold)
the player moves a little bit into the boundary
when my speed is low, something like 10 it’s not that bad
but when i’m on something high like 100
it moves 1 or 2 points behind the boundary
this is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMover : MonoBehaviour {
private Rigidbody Player;
public float limit = 5.5f; // boundary left is - and right is +
public int speed = 10;
public float forwardMovement = 1; // instead of inputAxis
public float leftAndRight = 1, upAndDown = 0, enableForward = 1;
void Start () {
Player = GetComponent<Rigidbody>();
}
void FixedUpdate() {
// multipliers will say which axis will have more effect than other .. and disable the axis that is multiplied by 0
Vector3 movement = new Vector3(Input.GetAxisRaw("Horizontal") * leftAndRight, Input.GetAxisRaw("Vertical") * upAndDown, forwardMovement * enableForward);
// normalizing to 1 and assigning speed
movement = movement.normalized;
Player.velocity = movement * speed ;
// setting position boundaries using clamp on x axis
transform.position = new Vector3(Mathf.Clamp(Player.position.x, -limit, limit), transform.position.y, transform.position.z);
}
}
i guess a way to solve it would be to target the movement.x exponentially by the speed
so that the speed would have a lesser effect on the x-axis the higher it is
and i tried to do that … but that would also mean that when i’m increasing speed in the z-axis by 3 or 4 i would be doing it b 0.5 or 1 and that would look ugly when I reach something like 100 . .
just so you know
added colliders … player was still ignoring colliders when speed is +80 … or +4000 when using delta time
turns out the problem is on the player’s rigid body so to solve it
i added to the player script
Player.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
the “Player” is the player’s rigid body
I know that feeling. Personally though, I would probably also shoot out a raycast or two each time the player tried to move in the direction of travel to check if there is a wall/collider there or not and make sure they can never touch it. This makes it handy should you decide later to have varying widths on your path.
you can also use a Bounds (a struct typically used on Renderers and colliders, but you can use it elsewhere) and then simply set the transform.position = bounds.ClosestPoint(transform.position); as the last thing in update.
and the transform will always remain within the defined bounds. I use it for my CameraTarget Gameobject to constrain the target to within a 3D box.
what happened is that i had to use colliders o solve my issue with boundaries … because like i said the more the speed the bigger the boundaries get … so that solved my issue … but then when the game object is speeding and its collider (3 cube colliders) touch the boundary Collider it would rotate the game object just a little bit … so then i added bounds and everything is working perfectly … so i is a combination between using bounds and adding colliders on the two sides right after the bounds … so that when the object is speeding it would touch the boundary, the boundary would decrease the speed and then the collider would stop the object from passing through …