[ad_1]
I’ve a code unity that strikes a goal utilizing joystick nonetheless the item goes up the ground it barely bounces off. Right here is my code
public class Motion : MonoBehaviour {
[Header("UI Elements")]
[SerializeField] personal Button buttonRunAndWalk;
[SerializeField] personal Picture imageRun;
[SerializeField] personal Picture imageWalk;
[Space(10)]
[Header("Game Objects or Others")]
[SerializeField] personal Joystick joystick;
[SerializeField] personal GameObject playerModel;
[SerializeField] personal NormalAttack normalAttack;
[Space(10)]
[Header("Variable Declarations and other assignments")]
[SerializeField] personal float maxSlopeAngle;
personal float turnSmoothTime = 0.0f;
personal float turnSmoothVelocity;
personal float speedSmoothTime = 0.1f;
personal float speedSmoothVelocity;
personal float currentSpeed;
personal bool isMoving;
personal bool isRunning = false;
personal bool isInSkillAnimation;
personal Rework cameraTransform;
personal Rigidbody playerRigidBody;
personal CharacterStatManager characterStatManager;
personal Vector3 currentTargetPosition;
personal RaycastHit slopeHit;
personal Vector3 directionToTarget;
/* Stat Modifiers Solely */
personal StatModifier addBasicSpeed;
public bool GetSetIsRunning {
get { return isRunning; }
set { isRunning = worth; }
}
public bool GetSetIsMoving {
get { return isMoving; }
set { isMoving = worth; }
}
public bool GetSetIsInSkillAnimation {
get { return isInSkillAnimation; }
set { isInSkillAnimation = worth; }
}
public Vector3 GetSetTargetPosition {
get { return currentTargetPosition; }
set { currentTargetPosition = worth; }
}
personal void Awake() {
}
personal void Begin() {
cameraTransform = Digicam.primary.remodel;
playerRigidBody = playerModel.GetComponent<Rigidbody>();
characterStatManager = GetComponent<CharacterStatManager>();
addBasicSpeed = new StatModifier(4, Tags.StatModType.Flat, this);
SetRunAndWalkUI();
buttonRunAndWalk.onClick.AddListener(() => {
GetSetIsRunning = !GetSetIsRunning;
if (GetSetIsRunning) {
characterStatManager.Velocity.AddModifier(addBasicSpeed);
} else {
characterStatManager.Velocity.RemoveModifier(addBasicSpeed);
}
SetRunAndWalkUI();
});
}
personal void Replace() {
if (currentTargetPosition != Vector3.zero) return;
Vector2 enter = new Vector2(
joystick.Horizontal,
joystick.Vertical
);
Vector2 inputDir = enter.normalized;
if (GetSetIsInSkillAnimation) {
inputDir = Vector2.zero;
}
GetSetIsMoving = inputDir != Vector2.zero;
if (isMoving) {
float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraTransform.eulerAngles.y;
playerRigidBody.remodel.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(
playerRigidBody.remodel.eulerAngles.y,
targetRotation, ref turnSmoothVelocity,
turnSmoothTime
);
}
float targetSpeed = characterStatManager.Velocity.Worth * inputDir.magnitude;
currentSpeed = Mathf.SmoothDamp(
currentSpeed,
targetSpeed,
ref speedSmoothVelocity,
speedSmoothTime
);
Vector3 velocity = playerRigidBody.remodel.ahead * currentSpeed;
playerRigidBody.velocity = new Vector3(velocity.x, playerRigidBody.velocity.y, velocity.z);
if (onSlope()) {
//What to do?
}
}
public void MoveTowardsTarget(Vector3 goal) {
if (!normalAttack.GetSetEnableMoving) {
if (currentTargetPosition == Vector3.zero) return;
}
directionToTarget = (goal - playerModel.remodel.place).normalized;
float targetSpeed = characterStatManager.Velocity.Worth;
currentTargetPosition = goal;
isMoving = true;
currentSpeed = Mathf.SmoothDamp(
currentSpeed,
targetSpeed,
ref speedSmoothVelocity,
speedSmoothTime
);
Vector3 velocity = directionToTarget * currentSpeed;
playerRigidBody.velocity = new Vector3(velocity.x, playerRigidBody.velocity.y, velocity.z);
if (onSlope()) {
//What to do?
}
}
personal bool onSlope() {
/*return Physics.Raycast(new Vector3(
playerRigidBody.remodel.place.x,
playerRigidBody.remodel.place.y + 0.5f,
playerRigidBody.remodel.place.z),
Vector3.down, 1
);*/
if (Physics.Raycast(playerModel.remodel.place,Vector3.down,out slopeHit,playerModel.remodel.lossyScale.y * 0.5f + 0.3f)) {
float angle = Vector3.Angle(Vector3.up, slopeHit.regular);
return angle < maxSlopeAngle && angle != 0;
}
return false;
}
personal Vector3 GetSlopeDirection() {
return Vector3.ProjectOnPlane(playerRigidBody.velocity, slopeHit.regular).normalized;
}
personal void SetRunAndWalkUI() {
imageRun.gameObject.SetActive(!GetSetIsRunning);
imageWalk.gameObject.SetActive(GetSetIsRunning);
}
}
[ad_2]