- Home /
rigidBody velocity acting up
i am making a crouching(more like sliding)script for my FPS, and there is an error. what is wrong?(the error is "Argument 1: cannot convert from 'UnityEngine.Vector3' to 'float' ") also if i delete "transform.forward" it seems to work, but it only sets a force in one direction.
using UnityEngine;
public class crouch : MonoBehaviour
{
public CharacterController player;
public Rigidbody rb;
public float boost = 120f;
void Start()
{
player = gameObject.GetComponent<CharacterController>();
rb = gameObject.GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetButton("left shift"))
{
player.height = 0.2f;
player.center = new Vector3(0f, 0.5f, 0f);
rb.velocity = new Vector3(transform.forward * boost * Time.deltaTime, rb.velocity.y, rb.velocity.z);
}
else
{
player.height = 2.1f;
player.center = new Vector3(0f, 0f, 0f);
rb.velocity = new Vector3(0f, 0f, 0f);
}
}
}.
the peticular line of code that has the error is:
rb.velocity = new Vector3(transform.forward * boost * Time.deltaTime, rb.velocity.y, rb.velocity.z);
Answer by cjmort10 · Nov 22, 2020 at 09:53 PM
Pretty Sure This Is Right Not Sure Though
You Can't Have A Vector And A Float On The Same Line So It Would Have To Be Like This
using UnityEngine;
public class crouch : MonoBehaviour
{
public CharacterController player;
public Rigidbody rb;
public float boost = 120f;
void Start()
{
player = gameObject.GetComponent<CharacterController>();
rb = gameObject.GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetButton("left shift"))
{
player.height = 0.2f;
player.center = new Vector3(0f, 0.5f, 0f);
rb.velocity = new Vector3(transform.forward * Time.deltaTime, rb.velocity.y, rb.velocity.z);
}
else
{
player.height = 2.1f;
player.center = new Vector3(0f, 0f, 0f);
rb.velocity = new Vector3(0f, 0f, 0f);
}
}
}.
Also If You Can Help Me With Mine
Answer by elz50 · Nov 23, 2020 at 12:56 AM
i got it now, just used a different line. thanx for ur help!
if u want the line just ask because i am too lazy to go right now.
Your answer
![](https://koobas.hobune.stream/wayback/20220613020329im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2d game - looking toward velocity for rigidbody player 1 Answer
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Rigid Body Velocity max 1 Answer
rigidbody.velocity.normalized application 1 Answer
Can't access the velocity of a rigidbody: 'Rigidbody' does not contain a definition for 'velocity' 1 Answer