Moving player left and right (HELP)
this two command problem var absVelX = Mathf.Abs (rigidbody2D.velocity.x); rigidbody2D.AddForce (new Vector2 (forceX, forceY));
UnityEng Comp does not contain a definition for velocity
UnityEng Comp does not contain a definition for ADDFORCE
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour {
public float speed = 10f;
public Vector2 maxVelocity = new Vector2(3, 5);
// Update is called once per frame
void Update () {
var forceX = 0f;
var forceY = 0f;
var absVelX = Mathf.Abs (rigidbody2D.velocity.x);
if (Input.GetKey ("right")) {
if(absVelX < maxVelocity.x)
forceX = speed;
transform.localScale = new Vector3(1, 1, 1);
} else if (Input.GetKey ("left")) {
if(absVelX < maxVelocity.x)
forceX = -speed;
transform.localScale = new Vector3(-1, 1, 1);
}
rigidbody2D.AddForce (new Vector2 (forceX, forceY));
}
}
Answer by berto654 · Mar 24, 2018 at 01:25 AM
void FixedUpdate(){
//this will make it move left and right.
rigidbody2D.volicty.x = Input.GetAxis ("Horizontal") * time.deltatime * speed;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612121756im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trying to add force to an object... is this done correctly? 0 Answers
Change transform.position into addForce or velocity 0 Answers
how get velocity from swipe fast or slow speed 1 Answer
Ball Speed is not increasing as per code 0 Answers
How to set rigidbody velocity and angularVelocity to Vector3.zero over time? 1 Answer