Moving Platform
Hello, I am making a 2.5D platformer game and now facing a few issues, I am relatively new to Unity.
I made a moving platform, and my player has a CharacterController Collider. When he lands on the moving platform he does not stick on the platform. However, When I change it to a box collider the player sticks to the platform. Here is the code of the moving platform. public Transform platform; public Transform startTrans; public Transform endTrans;
public float platSpeed = 2.5f;
private Rigidbody rb;
private Vector3 direction;
private Transform destination;
void Start()
{
rb = this.platform.GetComponent<Rigidbody>();
SetDestination(startTrans);
}
void FixedUpdate()
{
rb.MovePosition(platform.position + direction * platSpeed * Time.fixedDeltaTime);
if (Vector3.Distance(platform.position, destination.position) < platSpeed * Time.fixedDeltaTime)
{
SetDestination(destination == startTrans ? endTrans : startTrans);
}
}
void SetDestination(Transform dest)
{
destination = dest;
direction = (destination.position - platform.position).normalized;
}
The other issue is, I am trying to make a treadmill like platform, which pushes the player backwards, I am unsure on how to do this.
And Finally, I am trying to flip/rotate a 3D player to look the way is moving, e.g. look left if moving left, look right if moving right... I tried different methods such as eulanger, rotate and flip scale. But it causes the player to move/flip like crazy especially as it has animation.
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
Vector3 moveDirection = Vector3.zero;
Animator myAnim;
bool facingRight;
void Start()
{
myAnim = GetComponent<Animator>();
facingRight = true;
}
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(0, Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (moveDirection.z != 0) myAnim.SetBool("isRunning", true);
else myAnim.SetBool("isRunning", false);
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
myAnim.SetTrigger("isJumping");
}
if (moveDirection.x > 0 && !facingRight) Flip();
else if (moveDirection.x < 0 && facingRight) Flip();
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.z *= -1;
transform.localScale = theScale;
}
Many Thanks.
Your answer
Follow this Question
Related Questions
I can't rotate and move in the opposite direction with character Controller 0 Answers
Preserving the players rotation on rotating platforms 0 Answers
How to move my rigid body in the direction my model is facing. 0 Answers
Gravity To CharacterController 0 Answers
Question on Unity5 CharacterController performance/efficiency 0 Answers