No Script Errors - Character doesnt jump - Unity 3d - unity3d

Currently building a 2D game, and have created a javascript code for the jump for my character 'Ezio' but it does not do anything when i press 'space' for it to jump. There are no errors with the code either.
#pragma strict
var jump :float = 0;
var jumpspeed : float = 15;
var jumptimer :float = 0;
function Start () {
}
function Update () {
if (jump == 1) {
jumptimer = jumptimer +1;
if (jumptimer >= 50) {
jumptimer = 0;
jump = 0;
}
}
}
if (Input.GetKeyDown ("space"))
{
if (jump == 0) {
rigidbody2D.velocity.y = jumpspeed;
jump = 1;
}
}
Any suggestions on what could be the issue?

Try this:
#pragma strict
var jump :float = 0;
var jumpspeed : float = 15;
var jumptimer :float = 0;
function Start () {
}
function Update() {
if (Input.GetKeyDown("space")) {
if (jump == 1) {
jumptimer = jumptimer + 1;
if (jumptimer >= 50) {
jumptimer = 0;
jump = 0;
}
} else {
rigidbody2D.velocity.y = jumpspeed;
jump = 1;
}
}
}

Related

Lerp between point on LineRenderer

Im stuck on how to lerp between points of the linerenderer which are added on runtime. It should animate in order. So IEnumerator should be used i guess.
private void makeLine(Transform finalPoint)
{
if(lastPoints == null)
{
lastPoints = finalPoint;
points.Add(lastPoints);
}
else
{
points.Add(finalPoint);
lr.enabled = true;
SetupLine();
}
}
private void SetupLine()
{
int pointLength = points.Count;
lr.positionCount = pointLength;
for (int i = 0; i < pointLength; i++)
{
lr.SetPosition(i, points[i].position);
// StartCoroutine(AnimateLine());
}
}
I found a code example. But now sure how to implement it so it would fit the code above:
private IEnumerator AnimateLine()
{
//coroutinIsDone = false;
float segmentDuration = animationDuration / points.Count;
for (int i = 0; i < points.Count - 1; i++)
{
float startTime = Time.time;
Vector3 startPosition = points[i].position;
Vector3 endPosition = points[i + 1].position;
Vector3 pos = startPosition;
while (pos != endPosition)
{
float t = (Time.time - startTime) / segmentDuration;
pos = Vector3.Lerp(startPosition, endPosition, t);
for (int j = i + 1; j < points.Count; j++)
lr.SetPosition(j, pos);
yield return null;
}
}
}

GetPixel makes the game slow

I'm creating a cleaning game but in the getpixel line of code makes the game slow or having a delay. I need this line of code to detect the remaining dirt. How can I improve the code? or is there other way to detect remaining dirt?
private void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
if (touch.position != currentPosition || touch.position != prevPosition)
{
SetSinglePixel(touch.position);
prevPosition = touch.position;
}
}
else if (touch.phase == TouchPhase.Moved)
{
if (touch.position != currentPosition || touch.position != prevPosition)
{
currentPosition = touch.position;
Vector2 dif = currentPosition - prevPosition;
float distance = dif.magnitude;
if (distance > 36)
{
float count = distance / 36;
float n = distance / (count + 1);
float ddmo = 36;
for (int i = 0; i <= count; i++)
{
Vector2 gapPoint = Vector2.MoveTowards(prevPosition, currentPosition, ddmo * i);
if (Physics.Raycast(Camera.main.ScreenPointToRay(gapPoint), out RaycastHit hit))
{
gapPositions.Add(hit.textureCoord);
}
}
SetMultiplePixel(gapPositions);
gapPositions.Clear();
}
else
{
SetSinglePixel(currentPosition);
}
prevPosition = touch.position;
}
}
}
}
private void SetMultiplePixel(List<Vector2> givenPoints)
{
foreach (Vector2 pos in givenPoints)
{
SetPixel(pos.x, pos.y);
}
}
private void SetSinglePixel(Vector2 touchPos)
{
if (Physics.Raycast(Camera.main.ScreenPointToRay(touchPos), out RaycastHit hit))
{
SetPixel(hit.textureCoord.x, hit.textureCoord.y);
}
}
public void ResetTexture()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
private void SetPixel(float textureCoordX, float textureCoordY, bool isCheckRemainingTexture = false)
{
int pixelX = (int)(textureCoordX * _templateDirtMask.width);
int pixelY = (int)(textureCoordY * _templateDirtMask.height);
int pixelXOffset = pixelX - (_brush.width / 2);
int pixelYOffset = pixelY - (_brush.height / 2);
for (int x = 0; x < _brush.width; x++)
{
for (int y = 0; y < _brush.height; y++)
{
data = (pixelXOffset + x) + (pixelYOffset + y) * _templateDirtMask.width;
if (data > 0 && data < pixels.Length)
{
pixelDirt = _brush.GetPixel(x, y);// this code makes the game slow
Color pixelDirtMask = _templateDirtMask.GetPixel(pixelXOffset + x, pixelYOffset + y); // this code makes the game slow
float removedAmount = pixelDirtMask.g - (pixelDirtMask.g * pixelDirt.g);
dirtAmount -= removedAmount;
pixels[data] = new Color(0, pixelDirtMask.g * pixelDirt.g, 0);
}
}
}
ApplyTexture();
}

Unity Shooting script delay not working

I am making a shooter game in Unity. I wanted to delay the shooting script but now my code doesn't seem to be working. Any clues to how I might solve this? Other than the delay function everything worked fine.
Here is the code:
#pragma strict
private var player : GameObject;
public var speed : float;
private var bulletCounter : int;
var reloadtime : float = 2;
private var reloadTimer: float = 0.0;
function Start () {
player = this.gameObject;
}
function Update () {
if (reloadTimer > 0){
reloadTimer -= Time.deltaTime;
if (reloadTimer <= 0){
if(Input.GetKey("space")) {
Shoot();
}
}
if(Input.GetKey("w")){
if(player.transform.position.y < 20) {
player.transform.position.y += speed * Time.deltaTime;
}
}
if(Input.GetKey("s")){
if(player.transform.position.y > -20) {
player.transform.position.y -= speed * Time.deltaTime;
}
}
if(Input.GetKey("a")){
if(player.transform.position.x > -20) {
player.transform.position.x -= speed * Time.deltaTime;
}
}
if(Input.GetKey("d")){
if(player.transform.position.x < 20) {
player.transform.position.x += speed * Time.deltaTime;
}
}
}
function Shoot () {
bulletCounter++;
var bullet = GameObject.CreatePrimitive(PrimitiveType.Cube);
bullet.transform.position = player.transform.position;
bullet.AddComponent.<BulletScript>();
bullet.name = "Bullet"+bulletCounter.ToString();
var audio : AudioSource = GetComponent.<AudioSource>();
audio.Play();
reloadTimer = reloadtime;
}
}
You need to get movement code(W,S,A,D) outside the timer condition -
#pragma strict
private var player : GameObject;
public var speed : float;
private var bulletCounter : int;
var reloadtime : float = 2;
private var reloadTimer: float = 0.0;
function Start () {
player = this.gameObject;
}
function Update () {
if (reloadTimer > 0){
reloadTimer -= Time.deltaTime;
if (reloadTimer <= 0){
if(Input.GetKey("space")) {
Shoot();
}
}
}
/********* Move this outside the timer condition *********/
if(Input.GetKey("w")){
if(player.transform.position.y < 20) {
player.transform.position.y += speed * Time.deltaTime;
}
}
if(Input.GetKey("s")){
if(player.transform.position.y > -20) {
player.transform.position.y -= speed * Time.deltaTime;
}
}
if(Input.GetKey("a")){
if(player.transform.position.x > -20) {
player.transform.position.x -= speed * Time.deltaTime;
}
}
if(Input.GetKey("d")){
if(player.transform.position.x < 20) {
player.transform.position.x += speed * Time.deltaTime;
}
}
function Shoot () {
bulletCounter++;
var bullet = GameObject.CreatePrimitive(PrimitiveType.Cube);
bullet.transform.position = player.transform.position;
bullet.AddComponent.<BulletScript>();
bullet.name = "Bullet"+bulletCounter.ToString();
var audio : AudioSource = GetComponent.<AudioSource>();
audio.Play();
reloadTimer = reloadtime;
}
}
This will keep the movement enabled during wait period(reloadtime) after player shoots.
I think your idea is not so clear. Easiest way to do it would be.
function Shoot () {
...
reloadTimer = Time.time + reloadtime;
}
And if should look like this
if (reloadTimer >= Time.time){
if(Input.GetKey("space")) {
Shoot();
}
}

if statement with Input.GetButtonUp and > 0 doesnt return true

The statement if(Input.GetButtonUp("Heavy Attack") && chargeTime > 0) never returns true unless I am mashing heavy attack like a madman and I do not understand why?
var ID: int = 003;
var motionCounter: int[];
var currentMotion: int;
var timeBetweenButtons: float;
var chargeTime: float;
var isComboing: boolean;
function Awake() {
timeBetweenButtons = 0;
motionCounter = new int[10];
currentMotion = 0;
chargeTime = 0.7;
isComboing = false;
}
function Update() {
if (isComboing == true) {
timeBetweenButtons -= Time.deltaTime;
if (timeBetweenButtons <= 0) {
isComboing = false;
currentMotion = 0;
motionCounter[0] = 0;
Debug.Log("Combo Dropped");
}
}
if (currentMotion == 0) {
if (Input.GetButton("Heavy Attack")) {
chargeTime -= Time.deltaTime;
if (chargeTime <= 0) {
Debug.Log("Lunging Bite");
chargeTime = 0.7;
}
if (Input.GetButtonUp("Heavy Attack") && chargeTime > 0) {
chargeTime = 0.7;
motionCounter[currentMotion] = 2;
timeBetweenButtons = 1;
Debug.Log("Coiling Rattle");
}
if (Input.GetButtonDown("Light Attack")) {
timeBetweenButtons = 1;
motionCounter[currentMotion] = 1;
currentMotion++;
Debug.Log("Vipers_Tooth_01");
}
}
}
if (currentMotion > 0) {
if (Input.GetButtonDown("Light Attack")) {
motionCounter[currentMotion] = 1;
if (motionCounter[currentMotion] == 1 && motionCounter[currentMotion - 1] == 1) {
timeBetweenButtons = 1;
Debug.Log("Vipers_Tooth_02");
currentMotion = 0;
}
}
if (Input.GetButtonDown("Heavy Attack")) {
motionCounter[currentMotion] = 2;
}
}
}
var motionCounter : int[];
var currentMotion : int;
var timeBetweenButtons : float;
var chargeTime : float;
var isComboing : boolean;
function Awake()
{
timeBetweenButtons = 0;
motionCounter = new int[10];
currentMotion = 0;
chargeTime = 0.7;
isComboing = false;
}
function Update()
{
if(isComboing == true)
{
timeBetweenButtons -= Time.deltaTime;
if(timeBetweenButtons <= 0)
{
isComboing = false;
currentMotion = 0;
motionCounter[0] = 0;
Debug.Log("Combo Dropped");
}
}
if(currentMotion == 0)
{
if(Input.GetButton("Heavy Attack"))
{
chargeTime -= Time.deltaTime;
if( chargeTime <= 0)
{
Debug.Log("Lunging Bite");
chargeTime = 0.7;
}
if(Input.GetButtonUp("Heavy Attack") && chargeTime > 0)
{
chargeTime = 0.7;
motionCounter[currentMotion] = 2;
timeBetweenButtons = 1;
Debug.Log("Coiling Rattle");
}
if(Input.GetButtonDown("Light Attack"))
{
timeBetweenButtons = 1;
motionCounter[currentMotion] = 1;
currentMotion++;
Debug.Log("Vipers_Tooth_01");
}
}
}
if(currentMotion > 0)
{
if(Input.GetButtonDown("Light Attack"))
{
motionCounter[currentMotion] = 1;
if(motionCounter[currentMotion] == 1 && motionCounter[currentMotion - 1] == 1)
{
timeBetweenButtons = 1;
Debug.Log("Vipers_Tooth_02");
currentMotion = 0;
}
}
if(Input.GetButtonDown("Heavy Attack"))
{
motionCounter[currentMotion] = 2;
}
}
}
If You want charge time to be >0 then why are you doing this?
chargeTime -= Time.deltaTime;
shouldn't you be increasing your variable to correspond to the number of seconds the player is holding the button?
If i am right then this line should look like this:
chargeTime += Time.deltaTime;
GetButtonUp should not be executed inside of the GetButton statement. The rarity of reaching that code is so low that i am surprised it has ever hit for you at all.
For that matter i would take GetButtonDown out as well.
The execution of code goes in the order as follows:
GetButtonDown (one call)
GetButton (multiple calls)
GetButtonUp (one call)
so you can see that GetButtonDown calls before GetButton, therefore the code wont go into GetButton before hitting GetButtonDown

How Can I Reset my Car Movement if I return to my last respawn point?

function OnTriggerEnter(col : Collider){
if(col.tag == "Player")
{
player.transform.position = SpawnPoint.position;
audio.PlayOneShot(Sound);
VioSign.enabled = true;
if(pauseEnabled == false){
pauseEnabled = true;
AudioListener.volume = 1;
Time.timeScale = 0;
Screen.showCursor = true;
}
}
}
This is my respawnpoint script .
#pragma strict
var wheelFL : WheelCollider;
var wheelFR : WheelCollider;
var wheelRL : WheelCollider;
var wheelRR : WheelCollider;
var maxTorque : float = 50;
function Start(){
rigidbody.centerOfMass.y = -0.9;
}
function FixedUpdate () {
wheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
wheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
wheelFL.steerAngle = 20 * Input.GetAxis("Horizontal");
wheelFR.steerAngle = 20 * Input.GetAxis("Horizontal");
}
Here is my Car Control Script I want to stop the car after respawning again to the respawn point . I'm having a difficult time to solve it. please help me :)
If you just want to stop the car's current movement, you can simply set its velocity to zero:
function OnTriggerEnter(col : Collider){
if(col.tag == "Player")
{
player.transform.position = SpawnPoint.position;
player.gameObject.rigidBody.velocity = Vector3.zero;
audio.PlayOneShot(Sound);
VioSign.enabled = true;
if(pauseEnabled == false){
pauseEnabled = true;
AudioListener.volume = 1;
Time.timeScale = 0;
Screen.showCursor = true;
}
}
}