- Home /
How to click on ball to release?,Click on ball to release
Hello, I am creating a basic bowling game. When I play the game, the ball automatically gets released towards the pins. What script do I need to first move the ball (left or right arrows keys) and then click on the ball to release? Thanks. Please see below code I already have for forward movement
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BallMovement : MonoBehaviour { public Rigidbody rb;
public float forwardForce = 100f;
void FixedUpdate()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
}
},Hello. I am making a basic bowling game and I want to know how to click on the ball so it releases and hits the pins. I have added some code (see below) for force forward movement. However, when I play the game the ball releases automatically. I want to adjust the ball first (left/right arrow keys) and then click on the ball to release for it to hit the pins.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BallMovement : MonoBehaviour { public Rigidbody rb;
public float forwardForce = 100f;
void FixedUpdate()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
}
}
Answer by _Venus_ · Sep 05, 2018 at 05:15 PM
You can try handling it differently. For example: using System.Collections; using System.Collections.Generic; using UnityEngine; public class BallMovement : MonoBehaviour { public Rigidbody rb; public bool leftnright; public float forwardForce = 100f; public float leftForce; void FixedUpdate() { if(leftnright) { if(Input.GetKey(KeyCode.A)) { transform.postion.x *= leftforce; } if(Input.GetKey(KeyCode.D)) { transform.postion.x *= -leftforce; } if(Input.GetButtonDown("Fire1")) { leftnright = false; } } else if(!leftnright) { rb.AddForce(0, 0, forwardForce * Time.deltaTime); } } }
Please note that i havent tested this code yet, but i hope it can give you some insight.
@Venus Thank you for the help. I have changed the code to the above but there are errors on both "transform.postion.x *= leftforce" It shows a red line under position and leftforce. Is there any other simple code I can use?
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Making a bubble level (not a game but work tool) 1 Answer
Player moves to the middle of the room when the level starts (2d point and click) 2 Answers
Objects move toward the center of the screen right after it is created 1 Answer