i want to set maximum speed
i want to set maximum speed. What is my fault ? My English very very bad, sorry..
using UnityEngine;
using System.Collections;
public class platetet : MonoBehaviour {
//Movement Speed
public float speed = 2;
//Body Max Speed
public float maxSpeed = 50f;
//Flap Force
public float force = 100;
//JetpackForce
public float jetpackForce = 50;
// Use this for initialization
void Start () {
//Hareket edeceği yer
GetComponent<Rigidbody2D> ().velocity = Vector2.up * speed;
}
// Update is called once per frame
void Update ()
{
//Flap
if (Input.GetKeyDown (KeyCode.Space)) {
GetComponent<Rigidbody2D> ().AddForce (Vector2.up * force);
}
for (var i = 0; i < Input.touchCount; ++i) {
if (Input.GetTouch (i).phase == TouchPhase.Began)
GetComponent<Rigidbody2D> ().AddForce (Vector2.up * force);
foreach (Touch touch in Input.touches)
GetComponent<Rigidbody2D> ().AddForce (Vector2.up * jetpackForce);
if(GetComponent<Rigidbody2D>().velocity.magnitude > maxSpeed)
{
GetComponent<Rigidbody2D>().velocity = GetComponent<Rigidbody2D>().velocity.normalized * maxSpeed;
}
}
}
}
Oh my !
Here is your code with the right indentation. Pls indent it the next time :
void Update () {
//Flap
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space))
{
GetComponent ().AddForce (Vector2.up force);
}
for (var i = 0; i < Input.touchCount; ++i)
{
if (Input.GetTouch (i).phase == TouchPhase.Began)
GetComponent ().AddForce (Vector2.up force);
foreach (Touch touch in Input.touches)
GetComponent ().AddForce (Vector2.up jetpackForce);
if(GetComponent().velocity.magnitude > maxSpeed)
{
GetComponent().velocity = GetComponent().velocity.normalized maxSpeed;
}
}
}
Answer by Samdeman22 · Dec 18, 2015 at 01:34 PM
Well, there are numerous places where you have random spaces where there shouldn't be:
Vector2.up force
What is your intention here?
On the line which I think you are trying to limit the velocity (the case where the velocity magnitude is more than the maxspeed) you actually have incorrect syntax, as well as the fact that I'm not sure you are accessing the velocity component correctly (you would need to get the gameobjects
GetComponent().velocity = GetComponent().velocity.normalized maxSpeed;
Not only this, but there are spelling mistakes in places. I think you might benefit from using an editor which highlights errors like this. You can get Visual Studio Community edition for free from here https://www.visualstudio.com/en-us/products/visual-studio-community-vs.aspx
You might also benefit from following a tutorial that does similar things, creating simple movement etc. Take a look at brackeys on youtube as an example.
I'm sorry my code is here.. How can i do ?
using UnityEngine;
using System.Collections;
public class platetet : $$anonymous$$onoBehaviour {
//$$anonymous$$ovement Speed
public float speed = 2;
//Body $$anonymous$$ax Speed
public float maxSpeed = 50f;
//Flap Force
public float force = 100;
//JetpackForce
public float jetpackForce = 50;
// Use this for initialization
void Start () {
//Hareket edeceği yer
GetComponent<Rigidbody2D> ().velocity = Vector2.up * speed;
}
// Update is called once per frame
void Update ()
{
//Flap
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) {
GetComponent<Rigidbody2D> ().AddForce (Vector2.up * force);
}
for (var i = 0; i < Input.touchCount; ++i) {
if (Input.GetTouch (i).phase == TouchPhase.Began)
GetComponent<Rigidbody2D> ().AddForce (Vector2.up * force);
foreach (Touch touch in Input.touches)
GetComponent<Rigidbody2D> ().AddForce (Vector2.up * jetpackForce);
if(GetComponent<Rigidbody2D>().velocity.magnitude > maxSpeed)
{
GetComponent<Rigidbody2D>().velocity = GetComponent<Rigidbody2D>().velocity.normalized * maxSpeed;
}
}
}
}
Your answer
Follow this Question
Related Questions
Movement input problem? 1 Answer
Character Movement on Cylinder Surface 0 Answers
Isometric movement for MoveTowards 0 Answers
Stop movement on collision 1 Answer