Answer the question
In order to leave comments, you need to log in
I wrote the character movement code, but it does not work, what should I do?
For more than an hour I puzzled over why the code does not work. It all started with the fact that I wanted to make the character turn in the direction of movement (a 2D platformer game). As a result, the code does not work, no matter how I try to fix it. help me please
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetKeyDown (KeyCode.Space) && CanJump())
{
jump();
}
}
void FixedUpdate()
{
rb.velocity = new Vector2(Input.GetAxis("Horizontal") * 6f, rb.velocity.y);
if (rb.velocity >= 0) {
RotateRight();
}
else (rb.velocity < 0) {
RotateLeft();
}
}
void jump()
{
rb.AddForce(transform.up * 5f, ForceMode2D.Impulse);
}
void RotateLeft()
{
if (Input.GetAxis("Horizontal") < 0)
transform.localRotation = Quaternion.Euler(0, 0, 0)
}
void RotateRight()
{
if (Input.GetAxis("Horizontal") > 0)
transform.localRotation = Quaternion.Euler(0, 180, 0)
}
}
Answer the question
In order to leave comments, you need to log in
You don’t have the CanJump function, and you are trying to call it, which means that the code is either not yours or you don’t understand what you are writing.
void Update () {
float v = Input.GetAxis ("Vertical");
float h = Input.GetAxis ("Horizontal");
rb.velocity = transform.right * v * 3f;//подходит для игры с видом сверху
rb.angularVelocity = -h * 100f;
if (Input.GetKeyDown ("space")){
rb.Addforce (0, 500);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question