Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I am working on a game made in Unity Engine.
For Movement, I have used vector2. But for vector2, you need to spam the buttons in order for the player to move. So i tried "while" function to loop the process. Here is the main code
if (Input.GetKeyDown(KeyCode.W))
{
i = 5;
}
//test
if (Input.GetKeyUp(KeyCode.W))
{
i = 1;
}
while(i !=1)
{
rb.AddForce(Vector2.up * JumpForce);
}
However, when I run it the engine crashes. Why?
Just to let you know, there are no compiler errors.
In Unity, the physics movement should be done in FixedUpdate, and input checking in Update. Something like this:
[SerializeField] private Rigidbody _rb;
[SerializeField] private float _jumpForce;
private bool _addForce;
private void Update()
{
_addForce = Input.GetKey(KeyCode.W);
}
private void FixedUpdate()
{
if (_addForce) _rb.AddForce(Vector2.up * _jumpForce);
}
If you add a while in the Update, it will stuck in that frame, freezing the main loop, and the engine can't poll for new inputs, so your Update can't continue.
The repeated addforces might be a bit much for the PC.
Do this:
if (Input.GetKey(KeyCode.W))
{
rb.AddForce(Vector2.up * JumpForce);
}
This way it only moves when key is being held.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
So i use unity and trying to use Rigidbody scripts and popped up with 5 errors with CS1003.
Heres my code:
using UnityEngine;
public class Movement : MonoBehaviour
void Start ()
rb.useGravity false;
{ public Rigidbody rb;
void Update() {
rb.AddForce(0, 0, 1000*Time.deltaTime);
}
}
I tried putting brackets in certain places but couldnt figure out what to put.
using UnityEngine;
public class Movement : MonoBehaviour
{
public Rigidbody rb;
void Start()
{
rb.useGravity = false;
}
void Update()
{
rb.AddForce(0, 0, 1000 * Time.deltaTime);
}
}
I think you want it to look a little like the above.
You are correct that your bracket closure is one of the things stopping Unity from being able to compile your program.
It seems that you are not very familiar with C# syntax in general. I would suggest reading up on these basics, it will greatly help you in developing in Unity.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I want to a add force to an enemy when my bullet hits it. I want the force to push the enemy away. I was having trouble finding a tutorial about this, so I'm assuming it's quite simple, and I'm oversimplifying it.
Note: this is for Unity2D
This is the code I have currently:
if (collision.CompareTag("Bullet"))
{
{
Destroy(gameObject);
}
if (collision.transform.rotation = 180)
{
rb.AddForce(transform.right * knockback * -1, ForceMode2D.Impulse);
}
else
{
rb.AddForce(transform.right * knockback * 1, ForceMode2D.Impulse);
}
}
However, Unity is responding to this code with:
error CS0029: Cannot implicitly convert type 'int' to 'UnityEngine.Quaternion.'
What I'm getting from this error is that I need to convert an int to a Quaternion, but how do I do so?.
If there are ways to make this code neater, please let me know.
Apologies if this is a repeat question, but I did try to find others. More apologies if this is a formatting nightmare.
I'm assuming that collision.transform.rotation = 180 is the line it is erroring on.
In this case it doesn't make sense to compare an int to a Quaternion because they aren't the same units.
Because there are approaches far simpler than comparing a Quaternion in this use case, I'll leave out Quaternions in this answer in order to keep this response concise.
For figuring out which side the bullet hits on, it will be easier to compare to x positions of your enemy and bullet, like so:
if (collision.transform.position.x > transform.position.x)
{
// Bullet is on right
}
else
{
// Bullet is on left
}
Another way to approach this is to use the bullet's .right direction to apply the force.
Doing it this way will apply the force based on the direction the bullet is facing.
rb.AddForce(collision.transform.right * knockback, ForceMode2D.Impulse);
In both cases, the code decides on a direction to apply the knockback in, although in different ways.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This is my complete code, can someone help me.
public float rotationSpeed;
public Transform target, player;
float mouseX, mouseY;
void Start()
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode; ;
}
void LateUpdate()
{
cameraControl();
}
Assets\ThirPersonCamerController.cs(14,28): error CS0119: 'CursorLockMode' is a type, which is not valid in the given context
You need to pick a lock mode:
Cursor.lockState = CursorLockMode.Locked;
Your choices are None, Locked, and Confined, but Locked seems to best fit your needs. See the Unity reference at https://docs.unity3d.com/ScriptReference/CursorLockMode.html
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm following a unity practice from youtube and seem to have problems at 6:42.
here's the link: https://www.youtube.com/watch?v=gE7gc1sblUA/
and a screenshot:
and the script:
using UnityEngine;
public class player : MonoBehaviour {
public float jumpforce = 10f;
public rigidbody2D rb;
// Update is called once per frame
void Update() {
if (input.GetButtonDown("Jump") || Input.GetMouseButtonDown(0))
{
rb.velocity = Vector2.up * jumpforce;
}
}
}
How can I solve this?
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want my little cube to change to a bigger one when i click the upper arrow, and change back when i pres the down arrow. I have tried:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerSkift : MonoBehaviour {
public gameObject myObject1;
public gameObject myObject2;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.UpArrow))
{
myObject1.SetActive (false);
myObject2.SetActive (true);
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
myObject2.SetActive(false);
myObject1.SetActive(true);
}
}
}
When i try to run it it says:
Assets/PlayerSkift.cs(9,9): error CS0118: `UnityEngine.Component.gameObject' is a `property' but a `type' was expected
I have no idea what that means, so if you know it, or know how to do it in a other way. Please help.
public gameObject myObject1;
public gameObject myObject2;
the above code, as per this discussion, appears to have a typo.
gameObject should be GameObject.