Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) » I Need Help On A Script

I Need Help On A Script

View previous topic View next topic Go down  Message [Page 1 of 1]

1 I Need Help On A Script on Sat Aug 09, 2014 11:42 am

OneKnifeGames


{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
I am trying to make my character move sideways in the 2D version of Unity. But every time I use this script it moves me up and down. You can try it out for yourselves. I left the script down below to see if you guys can find my mistake. If someone can help, that'd be great.


#pragma strict

var moveLeft : KeyCode;
var moveRight : KeyCode;

var speed : float = 10;

function Update ()
{
if (Input.GetKey(moveLeft))
{
rigidbody2D.velocity.y = speed;
}
else if (Input.GetKey(moveRight))
{
rigidbody2D.velocity.y = speed *-1;
}
else
{
rigidbody2D.velocity.y = 0;
}
}

View user profile http://oneknifegames.weebly.com/

2 Re: I Need Help On A Script on Sat Aug 09, 2014 4:51 pm

Ninjaboy follower


Member [Lv. 3]
Member [Lv. 3]
The movement should be in the Z-axis. This takes care of both the horizontal and vertical movement Try this:
function FixedUpdate(){
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement;
}

View user profile

3 Re: I Need Help On A Script on Sat Aug 09, 2014 4:57 pm

Ninjaboy follower


Member [Lv. 3]
Member [Lv. 3]
you can multiply the rigidbody.velocity = movement; by a number to make the object move faster for example
rigidbody.velocity = movement * 20f;

View user profile

4 Re: I Need Help On A Script on Sat Aug 09, 2014 7:08 pm

OneKnifeGames


{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Ninjaboy follower wrote:The movement should be in the Z-axis. This takes care of both the horizontal and vertical movement Try this:
function FixedUpdate(){
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement;
}

Thank you for the script but there's one problem; it says that there are no semicolons in the script, for example, it says: UCE0001 ';' expected. Insert semicolon at the end. Even though there are semicolons. If you have a solution I'm all ears.

View user profile http://oneknifegames.weebly.com/

5 Re: I Need Help On A Script on Sun Aug 10, 2014 2:34 am

Ninjaboy follower


Member [Lv. 3]
Member [Lv. 3]
Try Double clicking on the error and it will take you to the line where it sensed the problem and if there is a semi-colon there erase it and add it again. If the problem occurs again hit me back

View user profile

6 Re: I Need Help On A Script on Sun Aug 10, 2014 2:37 am

Ninjaboy follower


Member [Lv. 3]
Member [Lv. 3]
OR try this instead, it is a C# script:

using UnityEngine;
using System.Collections;
void FixedUpdate(){
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
}

View user profile

7 Re: I Need Help On A Script on Sun Aug 10, 2014 10:27 am

OneKnifeGames


{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Ninjaboy follower wrote:OR try this instead, it is a C# script:

using UnityEngine;
using System.Collections;
void FixedUpdate(){
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
}
There's only one complier error in the script and since I'm not familiar with C# I don't understand it. The error:

A namespace can only contain types and namespace declarations.

The Line:


void FixedUpdate(){

View user profile http://oneknifegames.weebly.com/

8 Re: I Need Help On A Script on Wed Aug 13, 2014 1:43 pm

Ninjaboy follower


Member [Lv. 3]
Member [Lv. 3]
Oh Yeah Sorry. Try this when you create the scripts name it as "Hello" and copy paste the following code(Replace the whole code)

using UnityEngine;
using System.Collections;

public class Hello : MonoBehaviour {
void FixedUpdate(){
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
}

}

If you have another error don't hesitate to ask Very Happy

View user profile

9 Re: I Need Help On A Script on Thu Aug 14, 2014 8:13 pm

OneKnifeGames


{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Ninjaboy follower wrote:Oh Yeah Sorry. Try this when you create the scripts name it as "Hello" and copy paste the following code(Replace the whole code)

using UnityEngine;
using System.Collections;

public class Hello : MonoBehaviour {
void FixedUpdate(){
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
}

}

If you have another error don't hesitate to ask Very Happy
Hi again! One more error.
The name `speed' does not exist in the current context

I named the script "Hello" (without the quotation marks) so I don't know the problem.

View user profile http://oneknifegames.weebly.com/

10 Re: I Need Help On A Script on Fri Aug 15, 2014 1:44 am

Ninjaboy follower


Member [Lv. 3]
Member [Lv. 3]
Declare a float variable just above void FixedUpdate()

public float speed;

you can change this value in the inspector the larger number you assign the faster the object moves.

View user profile

11 Re: I Need Help On A Script on Fri Aug 15, 2014 7:46 pm

OneKnifeGames


{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Ninjaboy follower wrote:Declare a float variable just above void FixedUpdate()

public float speed;

you can change this value in the inspector the larger number you assign the faster the object moves.
 Neutral Hi again, another error! Yay!  Very Happy  Not.  Mad One more compiler error, 
Unexpected symbol `void' in class, struct, or interface member declaration.

I put the public float speed; above the FixedUpdate() too by the way

View user profile http://oneknifegames.weebly.com/

12 Re: I Need Help On A Script on Fri Aug 15, 2014 8:23 pm

OneKnifeGames


{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Follow up to my last post, i put a comma instead of a semi colon. Dumb mistake on my part. But now I'm running into problems such as rigidbody I don't know how you want me to set it up.

View user profile http://oneknifegames.weebly.com/

13 Re: I Need Help On A Script on Fri Aug 22, 2014 1:59 am

Ninjaboy follower


Member [Lv. 3]
Member [Lv. 3]
Select the player(object) click Add Component -> Physics-> Rigidbody

View user profile

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum