Help me with a bouncing platform for a 3d game
Hello, i've been doing a game based on the roll-a-ball tutorial and i want to create piston-like platforms so when the player touch any it will push the ball up to another floor, all the answers and tutorials i've found are for 2d and the script doesn't seem to work the same.
Any idea?
Answer by Trigary · Jan 29, 2016 at 04:24 PM
When the piston collides (see: OnCollisionEnter) with the ball, the piston's velocity should change, either by setting the "transform.velocity" to a Vector3 pointing upwards or by adding a force to it (AddForce).
A script attached to the piston should contain something like this:
void OnCollisionEnter(Collision collidedWithThis) {
if (collidedWithThis.transform.name == "Ball") {
transform.velocity = transform.up * speed; //speed is a number variable
}
}
But keep in mind, this is just an example and I did not test it, look for tutorials, they will teach you the basics. And 3D and 2D are almost the same, this stuff is very basic, one more axis will just make things look more complicated, but they are almost the same in this level.
Answer by Unidesky · Jan 30, 2016 at 03:20 AM
Thanks for the tip, i'm trying it now and keeps telling me:
Assets/Scripts/jumpPlatform.cs(12,23): error CS1061: Type UnityEngine.Transform' does not contain a definition for
velocity' and no extension method velocity' of type
UnityEngine.Transform' could be found (are you missing a using directive or an assembly reference?)
Granted, i'm not the greastest coder in the genre, in fact i copy/paste the piece of code you passed me over a new script and this happened. Also i started trying with other codes from examples of 2d jump scripts and nothing yet, i've look at the oncollision and ontrigger examples of the scripting api and i don't know how to mix them properly.
Again, any ideas?
Okay, that's me being an idiot, transforms (the things which only have a coordinate in space) don't have velocities. Rigidbodies have velocities:
Below $$anonymous$$onoBehaviour write this:
private Rigidbody rigid; //this will enable you to access the rigidbody anywhere in your script
In the start "section":
rigid = transform.GetComponent<Rigidbody> (); //This will set the rigid variable to the Rigidbody attached to the transform of this script
And then:
rigid.velocity = transform.up * speed;
Thanks, i've made a script from parts from this and others and now i have a functional script for bouncing platforms:
using UnityEngine;
using System.Collections;
public class jumpPlatform : $$anonymous$$onoBehaviour
{
public float speed = 5f;
public Rigidbody rigidPlayer;
private Rigidbody rigid;
void Start ()
{
rigid = transform.GetComponent<Rigidbody>();
}
void OnCollisionEnter(Collision collidedWithThis)
{
if (collidedWithThis.transform.tag == "Player")
{
rigidPlayer.velocity = transform.up * speed;
}
}
}
And i have removed the rigidbody from the platform as well. All you have to do is put the script in the platform object and the player object in the rigidPlayer part on the platform (prefab or each one) to make it work.