Can someone help me fix this jumpscript? - c#

Im trying to make my character jump in unity2D,the jump itself works but i cant properly check if my player touches the ground.
I get 2 errors in the unity console.
the first one :
Physics2D does not contain a defenition for OverLapArea.
the second error :
Vector2 does not contain a constructor that takes 3 arguments.
(also i apologise for my bad english,its not my native language)
The script :
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
public class jump : MonoBehaviour
{
public bool IsGrounded;
public LayerMask platfomLayer;
public Rigidbody2D rb2D ;
// Start is called before the first frame update
void Start()
{
rb2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
IsGrounded = Physics2D.OverLapArea (new Vector2(transform.position.x - 0.5f, transform.position.x - 0.5f),
new Vector2(transform.position.x + 0.5f, transform.position.y - 0, 51f),platfomLayer);
{
if (Input.GetKeyDown("space") && IsGrounded) rb2D.AddForce(transform.up * 2000f);
}
}
}

The errors are both correct.
OverLapArea is spelled correctly but capitalized wrong. Unity references are case-sensitive.
You are indeed trying to pass 3 values into a new Vector2. Look carefully at what you're passing into your second new Vector2 value. You have three values separated by commas within the parenthesis. It looks like you mis-typed "0.5f" as "0, 51f"

Related

SceneManager does not contain a definition

Got a problem with using the scene manager. Already had a using UnityEngine.SceneManagement still shows the error of both LoadScene and GetActiveScene. I don't have a class such as "SceneManager". Here is the code.
using UnityEngine.SceneManagement;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
bool alive = true;
public float speed = 5f;
public Rigidbody rb;
float horizontalInput;
float horizontalMultiplier = 1.7f;
private void FixedUpdate()
{
if (!alive) return;
Vector3 forwardMove = transform.forward * speed * Time.fixedDeltaTime;
Vector3 horizontalMove = transform.right * horizontalInput * speed * Time.fixedDeltaTime * horizontalMultiplier;
rb.MovePosition(rb.position + forwardMove + horizontalMove);
}
// Update is called once per frame
void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
if (transform.position.y < -5)
{
end();
}
}
public void end ()
{
alive = false;
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
Here is the error I have.
First of all try:
UnityEngine.SceneManagement.SceneManager.LoadScene( "myScene");
There can be 2 troubles:
First -
This happened to me. I had downloaded an asset off the store called Falling Leaves. It included a script called SceneManager. Namespaces should be better utilized for store assets. I'll have to take a look at the code to my own to make sure I'm not breaking someone else's project unintentionally.
Second - if you are wirtting kinda your own lib for unity check that all .dll / projects was imported or if you are writting code in folder that marked with .asmdef this is tool in unity that helps with faster compile and more separeted development (there) and if you are do smth with this asmdef file and checkbox "include unity engine" is not setted as true this is too can be reason so check pls
Same question was answered there https://forum.unity.com/threads/scenemanager-does-not-contain-a-definition-for-loadscene.388892/
I believe you have to be using UnityEngine; before actually using UnityEngine.SceneManagement
here's an example
using UnityEngine; using UnityEngine.SceneManagement;
the problem here is that your using a part or branch or something of EnityEngine before using UnityEngine itself

Character in Unity scene walking backwards?

I am currently working on the Flooded Grounds Unity Asset, I'm implementing an Alien NPC, with a script that tells it to follow the player.
The problem that I'm encountering is that the Alien turns his back to the player and I can't figure out why. I also tried rotating it by 180° on the Y axis (both in the transform and inside of the script using transform.rotate.y - when using the transform doesn't make any difference while using it in the update function just makes it constantly snap back and forth).
Here's a video of the issue:
https://drive.google.com/file/d/1tC9zJWKXXDuNZNZJbl2htBUgTPSTB4IG/view?usp=sharing
And here's the script that I'm using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyFollow : MonoBehaviour
{
public NavMeshAgent enemy;
public Transform Player;
private Rigidbody alienRb;
private Animator alienAnim;
// Start is called before the first frame update
void Start()
{
alienRb = GetComponent<Rigidbody>();
alienAnim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
enemy.SetDestination(Player.position);
if(enemy.velocity.magnitude > 0)
{
alienAnim.SetTrigger("Walk");
}
}
}
Any help would be very appreciated!
Hello Mazza ^^ First of all, I have to say that your alien monster is really cute :D Anyway, did you try transform.LookAt(Player.transform.position)? Also, transform.rotation is might not be affective, maybe you can try alien.transform.Rotate(0.0f, 90.0f, 0.0f, Space.Self); or transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y - 180f, transform.rotation.z)

C# Pong Game - problem with ball scripting

I was just copying a tutorial on YouTube on how to make the pong game and when it came to scripting the ball, there was an error for me but not the guy in the video.
I have no experience in C#, only Python. This is the error message:
Assets\Ball.cs(24,26): error CS0117: 'Random' does not contain a definition for 'range'
Anyone know what's wrong?
This is the script for it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
public float speed;
public Rigidbody2D rb;
//Start is called before the first frame update
void Start()
{
Launch();
}
//Update is called once per frame
void Update()
{
}
private void Launch()
{
float x = Random.range(0,2) == 0 ? -1 : 1;
float y = Random.range(0,2) == 0 ? -1 : 1;
rb.velocity = new Vector2(speed * x, speed * y);
}
}
You have to write Random.Range and not Random.range
You need to capitalize "range", Random.Range is a method and most methods start with a capital letter so instead of "Random.range", you need to type "Random.Range"
Here the script method just calls the "Launch()" method at initialization time, your "Lauch()" method is problematic, you can try to modify the "Random.range(0,2)" method to "Random.Range(0, 2)"

Camera following program

I'm making a 2D platformer, and I want the camera to follow the character once it gets to the center, but follow it only on the x axis. I've downloaded this code, and it worked, but it followed the character on both the x and the y, and it stuck the character in the same corner of the camera that it started. I tried to add an if statement so that the camera only started moving once the offset was equal to the player, but it didn't work. I got the error code 'cannot implicitly convert vector3 to bool'
Here is the code:
using UnityEngine;
using System.Collections;
public class CompleteCameraController : MonoBehaviour {
public GameObject player;
private Vector3 offset;
void Start ()
{
offset = transform.position - player.transform.position;
}
void LateUpdate ()
{
if (offset == player.transform.position)
{
transform.position = player.transform.position + offset;
}
}
}
The documentation seems to suggest that what you are doing is fine. But perhaps your code could be modified to fit this example? My gut says that this is going to give you the same error, but it could be worth trying.
https://docs.unity3d.com/ScriptReference/Vector3-operator_eq.html
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public Transform other;
void Example()
{
if (other && transform.position == other.position)
{
print("I'm at the same place as the other transform!");
}
}
}
You could also try using the Vector3.Equals() method instead if you're ok with doing an exact comparison.
https://docs.unity3d.com/ScriptReference/Vector3.Equals.html
You could also try implementing your own function to compare the two vectors.
https://answers.unity.com/questions/395513/vector3-comparison-efficiency-and-float-precision.html
I really can't see anything wrong with the code you've posted, is it possible the error exists in another file?

I can’t find my ”rigidbody” tab on the right Unity

Yo ! I was programming on Unity and I wanted to modify something about the collision detection but when I click on any object but my player 1 on the scene I can’t find that tab. Did I mess up somewhere ? I put a video for more info.
Video : https://www.youtube.com/watch?v=SnmnpgYWKUA&t=1s
// The script that makes the camera follow my player :
using UnityEngine;
public class FollowPerso1 : MonoBehaviour {
public Transform perso1;
public Vector3 offset;
// Use this for initialization
// void Start ()
// Update is called once per frame
void Update () {
transform.position = perso1.position + offset;
}
}
// The script that makes the collision possibles :1
using UnityEngine;
public class Perso1Collision : MonoBehaviour {
public Perso1Movement Movement;
void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.tag == “Obstacle”)
{
Movement.enabled = false;
}
}
}
// The script that makes the movements possible :
using UnityEngine;
public class Perso1Movement : MonoBehaviour {
public Rigidbody rb;
public float forwardForce = 2000f; // <– We declared a variable float to change out forwardForce (REVOIR)… E03
public float sidewaysForce = 500f; // REVOIR
// Use this for initialization
// void Start ()
// voidUpdate : Update is called once per frame (So the force ”speed” will depend on how many FPS your PC has)
void FixedUpdate () // FixedUpdate is better to calculate Physics in Unity (”makes stuff looks smoother when you collide with stuff”. Ref. Brackeys EP.2 HTMVGIU)
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime); // <– NOTE : Gotta understand Time.deltaTime better
if (Input.GetKey(“d”)) // <– QUESTION : Why not ”D” and ”d” instead ? :O
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey(“a”)) // <– QUESTION : Why not ”D” and ”d” instead ? :O
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange); // Understand ForceMode.VelocityChange better E06
}
}
}
Thanks!
Just add it? On the bottom of the inspector click on Add Component and either input RigidBody in the textfield on the top or search it under the physics category (should be the 1st entry)
Rigidbody is no default component, you have to intentionally add it to your GameObjects (infact only Transform is default, but the primitives like cubes ofc need a meshfilter and renderer to display, and conveniently also come with boxcollider "out of the box")
I hope it was that and I could help.
Edit:
If you just start out with unity I would suggest to you going over the official tutorials on the unity-website so you get a general idea of how everything works. They are quite well made.
Here is the Rigidbody tutorial for example: https://unity3d.com/de/learn/tutorials/topics/physics/rigidbodies?playlist=17120

Categories