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
Related
I'm new to Yonti, I'm trying to build a simple maze game, and I'm trying to make the player's object move, but I'm not been able to do that for a few hours.
This is the code I wrote down:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerMover : MonoBehaviour
{
public float speed;
public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
}
this is the player object:
I do not understand where I have a problem. I added the script to the player object and I get no error message. It just does not move, I do not understand why.I set "RigidBody" and gave speed to the object, I do not understand where there is an error.
Try turning off isKinematic on your rigidbody. The IsKinematic is used for animation/scripted moving with rb.position. In return, this will disable all forces on the rigidbody, causing your rb.AddForce to be ineffective.
If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore. (This is taken from documenation)
I would also be cautious of the Fixed Update, as it can loose Input signal with KeyDown and KeyUp; but the way you are using it should be fine.
Rigidbody probably interacts with terrain so; instead of using "rigidbody" you should use CharacterController https://docs.unity3d.com/ScriptReference/CharacterController.html
When you use public variables you always need to assign them in some ways because Unity will try to search from the Inspector firstly. You can simply fix your problem by setting the rigidbody to private and then assign it in the Start() like you were doing.
If you want to set the Rigidbody to be public you must assign it in the inspector by dragging the component of the rigidbody inside the inspector of the script
I have a rectangle player sprite with a Box Collider 2D and a Rigidbody2D attached. I also have a script for point-and-click movement attached to the player object (i.e. player moves to mouse click position). However as soon as the player character hits a collider, it starts to jitter rather than just fully stop. I don't know a lot about Unity physics other than what I've picked up in a few tutorials, so I'll include as much relevant information as I can.
The Rigidbody 2D component has all forces set to 0, except for mass being 0.0001. The body type is dynamic, and collision detection is set to continuous. My movement script looks like this, got it straight from a tutorial:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControls : MonoBehaviour
{
public float speed = 1;
private Vector3 target;
void Start()
{
target = transform.position;
}
void Update()
{
if (Input.GetMouseButtonDown(0)) {
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = transform.position.z;
}
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
}
Is there an easier way to implement smooth point-and-click movement?
I have a problem that main camera position doesn't change after CharacterController.SimpleMove() called. The task is to create scene where camera moves.
I have Main Camera game object with Character Controller and Script attached.
The issue is that nothing in vrCamera position changed after SimpleMove() called.
My question is what is wrong in this code. I suggest something wrong with binding between MainCamera object and CharacterController component, but I have spend a lot of time investigating and nothing working found.
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class VRLookWalk : MonoBehaviour {
public Transform vrCamera;
public float toggleAngle = 30.0f;
public float speed = 3.0f;
public bool moveForwad;
private CharacterController cc;
// Use this for initialization
void Start () {
cc = vrCamera.GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
if (vrCamera.eulerAngles.x >= toggleAngle && vrCamera.eulerAngles.x < 90.0f)
{
Vector3 forward = vrCamera.TransformDirection(Vector3.forward);
cc.SimpleMove(forward * speed);
}
}
}
You can't move the VR Camera, it's the SDK that determine the mainCamera position.
In order to move your camera you can just make a new GameObject as a parent of your mainCamera then move the parent GameObject
Try this. Your TransformDirection probably returns wrong vector.
Vector3 forward = vrCamera.transform.forward;
cc.SimpleMove(forward * speed);
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.
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!