Altri
I have problem.
When i apply this script
using UnityEngine;
using System.Collections;
public class Volo : MonoBehaviour {
public Vector2 jumpForce = new Vector2(0, 300);
float flightSpeed = 10.0f; // Set this speed to whatever you want.
float energyDecay = 1.0f; // Set this to whatever you want.
float energy;
const float maxEnergy = 100;
bool grounded;
void Update ()
{
Vector2 jumpForce = new Vector2(0, 300);
if (Input.GetKeyDown("space") && grounded) // Controls the initial jump
{
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
GetComponent<Rigidbody2D>().AddForce(jumpForce);
}
if (Input.GetKey("space") && energy > 0) // Controls flight for as long as space is held down
{
Vector2 v = GetComponent<Transform>().velocity; // Creates a temporary velocity variable
v.y = flightSpeed; // Sets the temporary variables Y velocity to the flightSpeed
GetComponent<Transform>().velocity = v; // Sets the character velocity to the flightSpeed
energy -= energyDecay; // Reduces energy by energyDecay every frame
}
}
void OnCollisionEnter(Collision col) // When the character collides with something
{
if(col.collider.tag == "ground") // If the object has the tag "ground"
{
energy = maxEnergy; // Reset energy to max
grounded = true;
}
}
void OnCollisionExit(Collision col) // When the character stops colliding with something
{
if(col.collider.tag == "ground") // If the object has the tag "ground"
{
grounded = false;
}
}
}
to the character,when i press play there is another error:Assets/scripts/Volo.cs(24,59): 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?)What should i do? I really need it in a few days. thanks in started.
Answer by Dave-Carlile · Oct 13, 2015 at 02:31 PM
If you look at Unity's documentation about the Transform class, you will see that there is indeed no velocity
property. What you're most likely looking for would be the Rigidbody or RigidBody2D component. In fact, if you look up in your code a little bit you'll see the velocity being set there on that very component.