Heres my code:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed;
public float right = KeyCode;
public float left = KeyCode;
void FixedUpdate ()
{
if(Input.GetKey(right))
{
rigidbody2D.velocity.x = speed;
}
else if(Input.GetKey (left))
{
rigidbody2D.velocity.x = speed*-1;
}
else
{
rigidbody2D.velocity.x = 0;
}
}
}
I don’t know why i am getting the error i have tried looking around on google but nothing seems to help me.
Thanks in advance for any help!
In C# you can’t just set the individual part of the vector like that. you need to do something like:
rigidbody2D.velocity = newVector2 (speed, rigidbody2D.velocity.y);
smitchell:
In C# you can’t just set the individual part of the vector like that. you need to do something like:
rigidbody2D.velocity = newVector2 (speed, rigidbody2D.velocity.y);
I am a complete beginner, how would you put that into my code?
Dantus
August 20, 2014, 11:55am
4
Line 8 and 9 make no sense.
8 is probably meant like that:
public Keycode right = KeyCode.RightArrow;
I am really confused i am trying to make a script for a snappy platformer where when you let go of the key the character just stops, I havent found a single way to do this
Dantus
August 20, 2014, 12:14pm
6
There are a lot of resources online where you can learn how to code in Unity. This one is pretty good:
/********************/ Facebook: http://facebook.com/DigitalGamingInstitute Twitter: @DGIStudents My Book: http://www.apress.com/9781430267522 /*******************/ Pledge: https://www.patreon.com/unity Codey's Lab: Download it FREE! off...
Reading time: 76 mins 🕑
Likes: 41 ❤
It is not specifically for 2D, but you will learn lots of basics that will help you later on.