FragMate CommunityLog in

FragMate - Software and Game Developer Community, including Resources, Forums, Marketplace & More.

I Need Help On A Script

Share

descriptionI Need Help On A Script

more_horiz
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;
}
}

_________________
-One Knife Games

descriptionRe: I Need Help On A Script

more_horiz
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;
}

descriptionRe: I Need Help On A Script

more_horiz
you can multiply the rigidbody.velocity = movement; by a number to make the object move faster for example
rigidbody.velocity = movement * 20f;

descriptionRe: I Need Help On A Script

more_horiz
@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.

_________________
-One Knife Games

descriptionRe: I Need Help On A Script

more_horiz
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

descriptionRe: I Need Help On A Script

more_horiz
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;
}

descriptionRe: I Need Help On A Script

more_horiz
@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(){

_________________
-One Knife Games

descriptionRe: I Need Help On A Script

more_horiz
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

descriptionRe: I Need Help On A Script

more_horiz
@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.

_________________
-One Knife Games

descriptionRe: I Need Help On A Script

more_horiz
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.
Permissions in this forum:
You cannot reply to topics in this forum