I am trying to do my player movement script in C# and for some reason, when I launch the game in unity it still doesn't work. I was wondering if after setting up the vectors in the script, if I need to say what button can be pressed to make the player move.
Here's my code that I have. (can't Post Pictures Currently)
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed;
public Rigidbody rb;
void start()
{
rb = GetComponent <Rigidbody> ();
}
void fixedupdate()
{
float movementHorizontal = Input.GetAxis ("Horizontal");
float movementVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (movementHorizontal, 0.0f, movementVertical);
rb.AddForce (movement * speed);
}
}
C# is case sensitive so if this is your exact code your functions will never be called. They need to be called Start() and FixedUpdate()
You have named your methods incorrectly; The case matters. Because of this, they never get called. Instead they should be:
void Start()
and
void FixedUpdate()
Unity doesn't throw any errors because it thinks what you've written are your own private methods.
Related
I was attempting to make a top down 2D shooter using Unity. My code contains no errors that I could see, RigidBody2D and PlayerMovement (code for the player to move) have been added to the sprite, and RigidBody2D has been added to the PlayerMovement. My move speed is set to 5. Please let me know what I can do to fix this issue!
Code:
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
public Rigidbody2D rb;
Vector2 movement;
void Update()
{
MovementInput();
}
private void FixedUpdate()
{
rb.velocity = movement * moveSpeed;
}
void MovementInput()
{
float mx = Input.GetAxisRaw("Horizontal");
float my = Input.GetAxisRaw("Vertical");
movement = new Vector2(mx, my).normalized;
}
}
Use your character sprite as the mouse cursor instead to write a code to make the player follow the mouse pos
If you really want to move your player via script this video will help you https://www.youtube.com/watch?v=0Qy3l3VuF_o
Have a good day :)
Check if your rigidbody2D's Body type is set to static, If it is then set it to kinematic or dynamic.
Also where did you import the script to?
Your code is not the problem, I tested it myself! Although you could move MovementInput(); to the FixedUpdate. Its not required though.
Edit: Image of the inspector as i have it if it helps
I'm a Unity beginner.
I have a problem with my script: in the picture above, Player item does not exist in Move script.
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
public float speed;
public Transform player;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
transform.Translate(x * speed * Time.deltaTime, y * speed * Time.deltaTime, 0);
Vector2 v2 = Camera.main.ScreenToWorldPoint(Input.mousePosition) - player.position;
player.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(v2.y, v2.x) * Mathf.Red2Deg);
}
}
I am not sure if I understand you right, but as this script is the script that belongs to the Player Gameobject, you don't need to specify "public Transform player".
The Gameobject class inherits from the Transform class, so it should work if you try:
Vector2 v2 = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(v2.y, v2.x) * Mathf.Red2Deg);
Maybe to make it more clear:
You don't need to have Player as an "item" that you pull into the according field in the inspector as it was the case if you relate to a gameobject which does not contain the "Move" script.
So, if you attach a script to a gameobject (in this case you attach the script to "Player"), then any methods you script, are directly affecting the gameobject the script belongs to.
E.g. try the following:
void Update() {
if(Input.GetKeyDown("space")) {
Destroy(gameObject);
}
}
You will see that the circle, which forms your player, will destroy itself when you hit the space bar. The parameter "gameObject" refers to the gameobject the script belongs to.
I hope that helps you to understand that you don't need to specify an extra GameObject that needs to be linked to the script, as long as the Gameobject you want to manipulate contains this script.
I'm new to Unity and to C#. I was trying to code a 2D platformer movement script, but for some reasons the code I'm creating doesn't work.
The script is referred to a circle. I've added "Rigidbody2D" and "Circle Collider 2D".
I've tried to use this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public Rigidbody2D rb;
public void FixedUpdate()
{
if (Input.GetKey(KeyCode.RightArrow))
{
rb.AddForce(10, 0, 0);
}
}
}
The code should give an hit to the circle to make it move right, but Visual Studio says that "rb.AddForce" is an error. Can you help me, please?
Are you sure you have actually referenced the rigidbody? Did you drag the rigidbody in the editor? If you have not, you could also say the following (if the script is attached to object that holds the rigidbody you would like to move):
private Rigidbody2D rb;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
1)Make sure your Rigidbody component is NOT set to Kinematic.
2)Depending on the mass and linear drag of the rigidbody, you would need to change the force you apply to it accordingly. The code may be working but you would not see the body moving if you do not apply enough force.
3)Addforce() expects a Vector as an argument. This is your problem.
public float thrust; //set in editor, this is how strong you will be pushing the object
private Rigidbody2D rb;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
if (Input.GetKey(KeyCode.RightArrow))
{
rb.AddForce(transform.right * thrust); //this will move your RB to the right while you hold the right arrow
}
}
4) Set a linear drag of your rigidbody so that it can actually stop after applying the force to it. In order to make it work, set the mass and linear drag both to 1 for example and then just experiment with the thrust variable, it will eventually start moving. After that you can reduce/increase the linear drag and the thrust until you achieve the desired effect.
BONUS
If you wish to use a Vector3D the way you have tried in your code, you could do the following and it will work too:
private void FixedUpdate()
{
if (Input.GetKey(KeyCode.RightArrow))
{
rb.AddForce(new Vector3(10, 0, 0)); //this will move your RB to the right while you hold the right arrow
}
}
Because of Rigidbody2D implementation it takes Vector2 in constructor as an argument instead of simple Rigidbody which can take Vector3 and Vector2 as Vector3. Consider Vector3 v3 = new Vector2(10, 0); and
Vector2 v2 = new Vector3(10, 0, 0);
Try this
rb.AddForce(new Vector2(10, 0));
or
rb.AddForce(new Vector3(10, 0, 0));
You need to add ForceMode2D.Impulse for it to work:
using UnityEngine;
public class TestControl : MonoBehaviour
{
public Rigidbody2D rb2d;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
rb2d.AddForce(new Vector2(5, 0 ), ForceMode2D.Impulse);
Debug.Log("RightArrow was Pressed");
}
}
}
You can find more info in here: https://www.studytonight.com/game-development-in-2D/right-way-to-move
I'm writing a script in C# in Unity that essentially functions as a switch to turn gravity on or off for a 2D Rigidbody. When the game starts, gravity should be 0 for the Rigidbody. Then when the user taps the space bar, gravity is supposed to increase to 3 (which is does). Then, however, when the player collides with a gameObject labeled 'InvisGoal', the player should teleport to another location and then gravity should be 0 again. However, the player always falls after coming in contact with InvisGoal and teleporting and I can't figure out why. This is my first project in C# so sorry for any obvious errors.. The script is here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallLaunch : MonoBehaviour {
private Rigidbody2D myRigidBody;
public GameObject Ball;
// Use this for initialization
void Start ()
{
myRigidBody = GetComponent<Rigidbody2D> ();
GetComponent<Rigidbody2D>().gravityScale = 0f;
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown ("Jump"))
{
GetComponent<Rigidbody2D> ().gravityScale = 3f;
}
}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "InvisGoal")
{
Ball.gameObject.GetComponent<Rigidbody2D>().gravityScale = 0f;
transform.position = new Vector3 (0.61f, 1.18f, 0f);
return;
}
}
}
Ball.gameObject.GetComponent<Rigidbody2D>().gravityScale = 0f;
This is likely what is causing the problem.
It sounds like the RigidBody2D you are referencing to in this line is not the same as the one you retrieved beforehand with GetComponent().
GetComponent returns the component of the GameObject you call it from. Therefore in the code I mentioned above,
Ball.gameObject.GetComponent<RigidBody2D>()
and
GetComponent<RigidBody2D>()
would give you an two different RigidBody2D component if the field Ball does not refer to the same GameObject your BallLaunch script is attached to.
[
Supposing BallLaunch script is attached to the Ball you want to set the gravity of (As picture above)
Simply change:
Ball.gameObject.GetComponent<Rigidbody2D>().gravityScale = 0f;
To
GetComponent<Rigidbody2D>().gravityScale = 0f;
Also, since you already referenced your RigidBody2D in your Start method to the field myRigidBody, you can replace all subsequent GetComponent with myRigidBody.
GetComponent<Rigidbody2D>().gravityScale = 0f;
To
myRigidBody.gravityScale = 0f;
I have been trying to make a movement script for my player in a 2D game but without success. I do not know why it is not working.
The problem is that the player isn't moving. I have a RigidBody attached and gravity on. (Not sure if gravity makes such a difference but I just thought to mention it.)
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public Rigidbody rb;
public float speed = 10;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
float mx = Input.GetAxisRaw("Horizontal");
float mz = Input.GetAxisRaw("Vertical");
Vector3 movement = new Vector3(mx, 0.0f, mz);
Debug.Log(movement);
rb.AddForce(movement * speed * Time.deltaTime);
}
}
You may wanna make sure you are adding enough force to actually cause the player to move. Try increasing the force variable incrementally until you see a change. Hope this helps!