Home>
I gave the player character a script to jump and move left and right in Unity, but when it falls off a cliff, it spins and falls off my head.
How can I prevent it from rotating on its own?
The script is as follows.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerManager: MonoBehaviour
{
Rigidbody2D rigid2D;
float jumpForce = 160f;
float walkForce = 30.0f;
float maxWalkSpeed = 2.0f;
private void Start ()
{
this.rigid2D = GetComponent<Rigidbody2D>();
}
private void Update ()
{
if (Input.GetKeyDown (KeyCode.Space))
{
this.rigid2D.AddForce (transform.up * this.jumpForce);
}
int key = 0;
if (Input.GetKey (KeyCode.RightArrow))
{
key = 1;
}
if (Input.GetKey (KeyCode.LeftArrow))
{
key = -1;
}
// Player speed
float speedx = Mathf.Abs (this.rigid2D.velocity.x);
// Speed limit
if (speedx<this.maxWalkSpeed)
{
this.rigid2D.AddForce (transform.right * key * this.walkForce);
}
// Invert according to the direction of movement
if (key! = 0)
{
transform.localScale = new Vector3 (key, 1, 1);
}
}
}
code
-
Answer # 1
-
Answer # 2
I checked the freez rotation of Rigid Body 2D and it was fixed.
Related articles
- c # - player production in unity (shooting game)
- c # - [unity] i want to make the camera follow the player in photon, but if there are multiple players, it will not follow
- c # - about player prefs for unity
- c # - i want the camera to follow the player character in a tps game, but if i make multiple players in multiplayer, the camera
- c # - i can't attach a script in unity (beginner)
- c # - if the player is killed, i want the enemy to wait for a while and restart from the continuation unity
- character tracking by camera using unity and c #
- c # - code error of script implemented by unity cannot be resolved
- unity - i want to get the game object with the field name specified by the character string
- c # - i want to get the text of unity toggle group
- c # - visual studio 2019 does not open when double-clicking a unity script
- unity c # error code indexoutofrangeexception: index was outside the bounds of the array
- c # - [unity] how to make a script that executes processing after a certain period of time
- c # - unity) i want to get the image size in prefab
- unity - i want to give a player a search enemy that reacts only to a specific object
- c # - unity delegate basic cs0246 error
- character fade-in in unity
- c # - i want to read a txt file with unity and store it in an array
- unity c # php json array passing
Related questions
- c # - in a script? error cs1056: unexpected character'?' is displayed even though there is no
- candidates are not displayed even if i hit a period in c # opened in unity
- c # - no value is assigned when using if statement
- c # - touch judgment of unity object
- c # - i don't understand the code below
- c # - i want to gradually expand the screen of the winning player once the victory or defeat is decided
- c # - i want to specify a different url from the script side for the video player component of unity
- c # - conversion from enum type to enum type
- c # - [unity] an unintended transition occurs in the state transition of animator controller
I checked the freez rotation of Rigid Body 2D and it was fixed.