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
Share
Go down
avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 64
Points : 1858
Rep : 3
Join date : 2013-12-21
Age : 15
Location : USA
View user profilehttp://oneknifegames.weebly.com/

I Need Help On A Script

on Sat Aug 09, 2014 11:42 am
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;
}
}
Member [Lv. 3]
Member [Lv. 3]
Posts : 37
Points : 1642
Rep : 19
Join date : 2014-06-14
View user profile

Re: I Need Help On A Script

on Sat Aug 09, 2014 4:51 pm
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;
}

Member [Lv. 3]
Member [Lv. 3]
Posts : 37
Points : 1642
Rep : 19
Join date : 2014-06-14
View user profile

Re: I Need Help On A Script

on Sat Aug 09, 2014 4:57 pm
you can multiply the rigidbody.velocity = movement; by a number to make the object move faster for example
rigidbody.velocity = movement * 20f;
avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 64
Points : 1858
Rep : 3
Join date : 2013-12-21
Age : 15
Location : USA
View user profilehttp://oneknifegames.weebly.com/

Re: I Need Help On A Script

on Sat Aug 09, 2014 7:08 pm
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.
Member [Lv. 3]
Member [Lv. 3]
Posts : 37
Points : 1642
Rep : 19
Join date : 2014-06-14
View user profile

Re: I Need Help On A Script

on Sun Aug 10, 2014 2:34 am
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
Member [Lv. 3]
Member [Lv. 3]
Posts : 37
Points : 1642
Rep : 19
Join date : 2014-06-14
View user profile

Re: I Need Help On A Script

on Sun Aug 10, 2014 2:37 am
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;
}
avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 64
Points : 1858
Rep : 3
Join date : 2013-12-21
Age : 15
Location : USA
View user profilehttp://oneknifegames.weebly.com/

Re: I Need Help On A Script

on Sun Aug 10, 2014 10:27 am
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(){
Member [Lv. 3]
Member [Lv. 3]
Posts : 37
Points : 1642
Rep : 19
Join date : 2014-06-14
View user profile

Re: I Need Help On A Script

on Wed Aug 13, 2014 1:43 pm
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
avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 64
Points : 1858
Rep : 3
Join date : 2013-12-21
Age : 15
Location : USA
View user profilehttp://oneknifegames.weebly.com/

Re: I Need Help On A Script

on Thu Aug 14, 2014 8:13 pm
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.
Member [Lv. 3]
Member [Lv. 3]
Posts : 37
Points : 1642
Rep : 19
Join date : 2014-06-14
View user profile

Re: I Need Help On A Script

on Fri Aug 15, 2014 1:44 am
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.
avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 64
Points : 1858
Rep : 3
Join date : 2013-12-21
Age : 15
Location : USA
View user profilehttp://oneknifegames.weebly.com/

Re: I Need Help On A Script

on Fri Aug 15, 2014 7:46 pm
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
avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 64
Points : 1858
Rep : 3
Join date : 2013-12-21
Age : 15
Location : USA
View user profilehttp://oneknifegames.weebly.com/

Re: I Need Help On A Script

on Fri Aug 15, 2014 8:23 pm
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.
Member [Lv. 3]
Member [Lv. 3]
Posts : 37
Points : 1642
Rep : 19
Join date : 2014-06-14
View user profile

Re: I Need Help On A Script

on Fri Aug 22, 2014 1:59 am
Select the player(object) click Add Component -> Physics-> Rigidbody
Back to top
Permissions in this forum:
You cannot reply to topics in this forum