what is wrong with code there, the line: rb.velocity = Rigidbody. velocity.normi
ID: 3744311 • Letter: W
Question
what is wrong with code there, the line: rb.velocity = Rigidbody. velocity.normilized: shows an error?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//This is used for Unity 5, Here you will create a c# code that will check the current y value on the rigid body.
//If the y-value is higher than the existing, overwrite it. (That way you will only store the maximum y-value).
public class Speed : MonoBehaviour
{
public Rigidbody rb;
public float y = 100f;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate() {
if (rb.velocity.magnitude > y)
{
rb.velocity = Rigidbody.velocity.normalized;
}
}
}
Explanation / Answer
You need to notice that velocity is a property of RigidBody and the expression Rigidbody.velocity.normalized; is not a valid expression.
Alternatively, you can use
A more detailed example is given below.
public class Speed : MonoBehaviour
{
public Rigidbody rb;
public float y = 100f;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate() {
if (rb.velocity.magnitude > y)
{
rb.velocity = new Vector3(0, 10, 0);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.