- Home /
Question by
AnthonySpering · May 16 at 03:57 PM ·
collisioncontrollerspeedplatformer
How to move player after hitting object
So I am making a 3d platformer game in unity, and I want to make a section where when I hit one of the platforms it sends me shooting off it. This is my code for my move script
private void Start()
{
controller = gameObject.AddComponent<CharacterController>();
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("JumpPad"))
{
jumpHeight = JumpPad;
}
if (collision.gameObject.CompareTag("FallPad"))
{
jumpHeight = 3f;
}
if (collision.gameObject.CompareTag("GravityPad") && Input.GetButtonDown("Jump"))
{
gravityValue = 3;
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
Debug.Log("Jump");
}
if (collision.gameObject.CompareTag("PickUp"))
{
collision.gameObject.SetActive(false);
}
}
void Update()
{
groundedPlayer = controller.isGrounded;
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
controller.Move(move * Time.deltaTime * playerSpeed);
if (move != Vector3.zero)
{
gameObject.transform.forward = move;
}
// Changes the height position of the player..
if (Input.GetButtonDown("Jump") && groundedPlayer)
{
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
Debug.Log("Jump");
}
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
}
}
I want to change the player's velocity so that it goes, I've tried other code but none of them work.
Comment
You could try adding a Physic Material to the platform and increase its bounciness. Physic Material