質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

721閲覧

c#への書き直し

miiichat

総合スコア72

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2018/02/16 08:22

編集2018/02/16 09:10

こちらのサイトで勉強しています。
https://openbook4.me/projects/161/sections/1023
サイトではjsで書かれているのでそれをc#で書き直して勉強しています。

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Component; public class Ball : MonoBehaviour { public float Speed = 15.0f; void Start () { Rigidbody rigidbody = GetComponent<Rigidbody>(); } void Update () { if (Input.GetButtonUp("Jump") && rigidbody.velocity == new Vector3(0, 0, 0)){ rigidbody.AddForce((transform.forward + transform.right) * Speed, ForceMode.VelocityChange); } } }
var Speed : float = 15.0; function Update () { if (Input.GetButtonUp("Jump") && rigidbody.velocity == Vector3(0, 0, 0)){ rigidbody.AddForce((transform.forward + transform.right) * Speed, ForceMode.VelocityChange); } }
Assets/Ball.cs(4,1): error CS0138: A `using' directive can only be applied to namespaces but `UnityEngine.Component' denotes a type. Consider using a `using static' instead

たぶん初歩的なミスか、書き間違いだと思いますが分かりませんでした。

回答していただけると嬉しいです。

###追記

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Ball : MonoBehaviour { public float Speed = 15.0f; void Start () { Rigidbody rigidbody = GetComponent<Rigidbody>(); } void Update () { if (Input.GetButtonUp("Jump") && rigidbody.velocity == new Vector3(0, 0, 0)){ rigidbody.AddForce((transform.forward + transform.right) * Speed, ForceMode.VelocityChange); } } }
Assets/Ball.cs(9,20): warning CS0219: The variable `rigidbody' is assigned but its value is never used
Assets/Ball.cs(14,48): error CS1061: Type `UnityEngine.Component' does not contain a definition for `velocity' and no extension method `velocity' of type `UnityEngine.Component' could be found. Are you missing an assembly reference?
Assets/Ball.cs(15,20): error CS1061: Type `UnityEngine.Component' does not contain a definition for `AddForce' and no extension method `AddForce' of type `UnityEngine.Component' could be found. Are you missing an assembly reference?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

「using UnityEngine.Component;」はクラスなので、
「using static」にするか消して見てください。

「using UnityEngine;」しているので、おそらく消すのが正解かと思います。

追記
今手元に環境がないので、下記みたいにクラス変数でもつのが良いかと
※もしかしたらstaticじゃないとだめ?

C#

1 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5 6public class Ball : MonoBehaviour { 7 public float Speed = 15.0f; 8 private Rigidbody rigidbody = null; 9 10 void Start () { 11 this.rigidbody = GetComponent<Rigidbody>(); 12 } 13 14 15 void Update () { 16 if (Input.GetButtonUp("Jump") && this.rigidbody.velocity == new Vector3(0, 0, 0)){ 17 this.rigidbody.AddForce((transform.forward + transform.right) * Speed, ForceMode.VelocityChange); 18 } 19 } 20}```

投稿2018/02/16 08:28

編集2018/02/16 09:14
himakuma

総合スコア952

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

miiichat

2018/02/16 08:49

回答ありがとうございます! using UnityEngine.Component; を消して using System.Collections; using System.Collections.Generic; using UnityEngine; public class Ball : MonoBehaviour { public float Speed = 15.0f; void Start () { // Rigidbody rigidbody = GetComponent<Rigidbody>(); } void Update () { if (Input.GetButtonUp("Jump") && GetComponent<Rigidbody>().velocity == new Vector3(0, 0, 0)){ GetComponent<Rigidbody>().AddForce((transform.forward + transform.right) * Speed, ForceMode.VelocityChange); } } } としたら動きました。 rigidbodyをStartで定義するにはどうすれはいいですか?
miiichat

2018/02/16 09:01

毎回コンポーネント取得はどうかなと思って。 startで定義しておきたいんですがエラーが出ます!
himakuma

2018/02/16 09:06

どんなエラーでしょうか?
miiichat

2018/02/16 09:10

ありがとうございます。 追記しました!
miiichat

2018/02/16 09:31

出来ました! 本当にありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問