2014-12-01, 19:36
  #1
Medlem
Tja! Nu är det så här att jag har börjat på ett fågelspel i unity. Det började med att jag bara ville se hur mycket man kunde göra utan några för kunskaper. sååå jag satte igång med ett spel som jag sedan gjord om och om igen då det har blivit dåligt flera gånger. Men just den här gången blir världen skit bra!! Dock som sagt har jag inga (för)kunskaper till detta! Jag satte igång att läsa en bok då jag fick lite mer intresse för kodning i unity C#! Nu vill jag utvecklas och veta mer om kodning så jag tänkte fråga hur man kan göra ett double jump script i c# till unity! Jag kan lite basics till c# typ variablar, arrays, något movement, olika axis m.m! Såå kunde någon snälla skriva typ ett double jump script bara för att jag ska veta hur jag kan göra och så! Tacksam för svar

Citera
2014-12-01, 19:47
  #2
Medlem
poolos avatar
Posta ditt nuvarande script för fågeln
Citera
2014-12-01, 19:50
  #3
Medlem
Citat:
Ursprungligen postat av poolo
Posta ditt nuvarande script för fågeln




skratta inte! haha

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour {


public float speed = 5.0f;
public float jump = 2.0f;
public float down = 1.0f;
public float downforce = 0.3f;






void Start () {

}


void Update () {




transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * speed);
transform.Translate (Vector3.forward * Input.GetAxis("Vertical") * speed);
transform.Translate (Vector3.up * Input.GetAxis ("Jump") * jump);
transform.Translate (Vector3.down * Input.GetAxis("Vertical") * downforce);







}
}
__________________
Senast redigerad av JakobAtone 2014-12-01 kl. 19:53.
Citera
2014-12-01, 22:08
  #4
Medlem
poolos avatar
Skulle använda mig av rigidbody (rigidbody2D om du kör 2D) för att förflytta fågeln om jag var du. Då förflyttar du fågeln genom t.ex. AddForce(), blir enklare att få till bra rörelser.

Men här har du en börjar på hur dubbelhopp kan se ut.

Kod:
using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour
{
    public float speed = 5.0f;
    public float jump = 2.0f;
    public float down = 1.0f;
    public float downforce = 0.3f;

    public float jumpHeight = 20f;
    public float jumpCooldown = 2f;

    private float currentCooldown = 0f;
    private int currentJumpsLeft = 2;
    private jumpState currentBirdState = jumpState.ready;
    private Vector3 goalHeight;

    enum jumpState
    {
        ready,
        jumping,
        cooldown
    }

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * speed);
        transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * speed);

        if (Input.GetButtonDown("Jump"))
        {
            // Vi har hopp kvar och är redo att hoppa
            if (currentJumpsLeft > 0 && this.currentBirdState == jumpState.ready)
            {
                this.currentBirdState = jumpState.jumping;
                this.currentJumpsLeft -= 1;
                this.goalHeight = this.transform.position + Vector3.up * jumpHeight;
            }
        }

        if (this.currentBirdState == jumpState.jumping)
        {
            transform.Translate(Vector3.up * jump);

            // har vi nått höjd-destinationen så slutar vi att hoppa
            if (transform.position.y > this.goalHeight.y)
            {
                if (this.currentJumpsLeft == 0)
                {
                    // Vi har inga hopp kvar, vänta på cooldown
                    this.currentBirdState = jumpState.cooldown;
                }
                else
                {
                    // Vi har hopp kvar och är redo att hoppa igen
                    this.currentBirdState = jumpState.ready;
                }
            }
        }
        else
        {
            // Om vi inte hoppar så faller vi
            transform.Translate(Vector3.down * downforce);
        }

        // Ifall vi har slut på hopp så väntar vi 'jumpCooldown' sekunder
        if (this.currentBirdState == jumpState.cooldown)
        {
            currentCooldown += Time.deltaTime;
            if (currentCooldown > this.jumpCooldown)
            {
                this.currentCooldown = 0;
                this.currentBirdState = jumpState.ready;
                this.currentJumpsLeft = 2;
            }
        }
    }
}
Citera
2014-12-02, 19:33
  #5
Medlem
tja! Har själv fixat ett bra script! om någon undrar så är scriptet här :


using UnityEngine;
using System.Collections;

public class Moveing : MonoBehaviour {


public float force = 50.0f;
public float fSpeed = 30.0f;

// Use this for initialization
void Start () {

}

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

if (Input.GetKey("space"))
{
rigidbody.AddForce(Vector3.up * force);
}

if (Input.GetKey(KeyCode.W))
{
rigidbody.AddRelativeForce (Vector3.forward * fSpeed);
}

if (Input.GetKey(KeyCode.S))
{
rigidbody.AddRelativeForce (Vector3.back * fSpeed);
}

if (Input.GetKey(KeyCode.LeftShift))
{
rigidbody.AddForce (Vector3.down * fSpeed);
}
if (Input.GetKey(KeyCode.A))
{
rigidbody.AddRelativeForce (Vector3.left * fSpeed);
}
if (Input.GetKey(KeyCode.D))
{
rigidbody.AddRelativeForce (Vector3.right * fSpeed);
}




}
}
Citera

Skapa ett konto eller logga in för att kommentera

Du måste vara medlem för att kunna kommentera

Skapa ett konto

Det är enkelt att registrera ett nytt konto

Bli medlem

Logga in

Har du redan ett konto? Logga in här

Logga in