Domanda

So I managed to fix up my trampoline code so that the player will bounce up as if they're on a trampoline. However there are 2 problems I'm having with this code.

Firstly when bouncing on the trampoline it basically launches the player right into the stratosphere and they just keep going never stopping as if there is no gravity to bring them down. Even though I have gravity checked in the player rigidbody settings.

And secondly even if the player is in no where near the trampoline e.g. on a platform that has not got the trampoline script code attached to it will cause the player to be flung up into the air on immediate starting of the game.

The code is as follows below:

using UnityEngine;
using System.Collections;

public class small_trampoline_bounce : MonoBehaviour
{

bool willBounce = false;
float bounceHeight = 10;
public Transform Player;


// Use this for initialization
void Start()
{
}

// Update is called once per frame
void Update()
{
    Vector3 velocity = Player.rigidbody.velocity;

    if (willBounce)
    {

        Player.rigidbody.velocity = new Vector3(velocity.x, 0, velocity.z);

        Player.rigidbody.AddForce(0, bounceHeight, 0, ForceMode.Impulse);


        willBounce = false;
    }
}

void OnCollisionEnter(Collision Trampoline)
{

    if (Trampoline.gameObject.name == "Player")
    {
        willBounce = true;
    }

}

}

Also this is the current status of settings for both the trampoline and the player

Player settings: http://gyazo.com/b4d924849a86e5158361f6081948e39f.png

Trampoline settings: http://gyazo.com/ded2884b93dd53c9e1b7fabc57e4fe51.png

È stato utile?

Soluzione 2

Okay after messing around with the code I've managed to get it working properly, the end result being that I ended appending code to the player charactermotor instead so that when the player collider with the trampoline then it implemented a downward force which would reverse the bounce of the player to a specific height stated within the unity inspector. The code below shows the end result.

public float speed;
public float jumpSpeed; 
public float gravity;
public Vector3 moveDirection = Vector3.zero;
public float downwardForce;
public float terminalVelocity;
public float airSpeed;

void Update() 
{
    CharacterController controller = GetComponent<CharacterController>();
    // is the controller on the ground?
    if (controller.isGrounded) 
    {
        //Feed moveDirection with input.
        moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
        moveDirection = transform.TransformDirection (moveDirection);
        downwardForce = 0;
        //Multiply it by speed.
        moveDirection *= speed;

        //Jumping
        if (Input.GetButton ("Jump"))
        {
            downwardForce -= jumpSpeed;
        }   
    } 
    else 
    {
        if (downwardForce < terminalVelocity)
        {
                moveDirection *= airSpeed;
                downwardForce += gravity * Time.deltaTime;
        }
    }
    //Applying gravity to the controller
    moveDirection.y -= downwardForce * Time.deltaTime;
    //Making the character move
    controller.Move(moveDirection * Time.deltaTime);
}
void OnTriggerEnter(Collider collider)
{           
    if (collider.tag == "smallTrampoline") 
    {
        downwardForce *= -1 * collider.gameObject.transform.GetComponent<SmallTrampolineBounce> ().jumpSpeed;

    }
    if (collider.tag == "largeTrampoline") 
    {
        downwardForce *= -2 * collider.gameObject.transform.GetComponent<SmallTrampolineBounce> ().jumpSpeed;
    }

I also implemented airSpeed as a way of enforcing some speedy movement for the player when they're in the air as beforehand the player wasn't able to move at all and simply stayed stuck jumping up & down

Altri suggerimenti

I don't know if this will fix your issue, but perhaps try this:

using UnityEngine;
using System.Collections;

public class small_trampoline_bounce : MonoBehaviour
{

float bounceHeight = 10;


// Use this for initialization
void Start()
{
}

// Update is called once per frame
void Update()
{
}

void OnCollisionEnter(Collision Trampoline)
{

    if (Trampoline.gameObject.name == "Player")
    {
        Vector3 velocity = Trampoline.gameObject.rigidbody.velocity;
        Trampoline.gameObject.rigidbody.velocity = new Vector3(velocity.x, 0, velocity.z);

        Trampoline.gameObject.rigidbody.AddForce(0, bounceHeight, 0, ForceMode.Impulse);
    }

}

}

This way, the addforce is only ever applied to the player hitting the trampoline, and should trigger only once. It may not solve your problem, since your code theoretically should work, but you never know.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top