Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

C# one trigger for several actions

Discussion in 'Scripting' started by FatherAndMan, Jun 2, 2017.

  1. FatherAndMan

    FatherAndMan

    Joined:
    Apr 15, 2017
    Posts:
    3
    I have two objects and a game clock and two billboards: "you lose," and "you win." One object is player and the other is a target. The game is in walls but for a hole. The Trigger is just outside the hole.

    I'm trying really hard to "get" coding, but I need help. Player object hits the trigger, I'd like "you lose" to turn red, the clock to continue and an option to quit or reset the game up to two times (three losses total to new game or quit option). Target object hits, "you win," clock stops, and new game option or quit.

    So, material change, clock stopping or continuing and reset or new game all from one object or other hitting a trigger. I already have rudimentary Player movement on keyboard and Rift controller.

    C# syntax and relations are alien to me for now (I'm watching tuts). I'll try not to ask dense questions but my terminology is weak at the moment. Thanks for pitching in if you can.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I think you should look into each piece that you want to do separately. Then, try to tie them together.

    If the scripting part is (/ continues to be a tough spot), check out the Learning section on the Unity website for scripting. That will help you a lot, with this & future code.

    After those steps, if you write some code and it's not working or partially working, etc.. Return with your code posted, and what's happening and what the desired outcome is, and get some feedback :)
     
  3. FatherAndMan

    FatherAndMan

    Joined:
    Apr 15, 2017
    Posts:
    3
    OK, one step at a time. I have to begin with the messiest bit then. "Unity - Simple Clock" is a great tutorial here. It uses Now in DateTime to show real world time.

    I've seen the theories of Time.time and time and fixedTime return time since start in seconds and DateTime.second is supposed to be a thing according to Microsoft but MS Visual Studio says that's not so.

    How can I grab the seconds since start of play and make them affect the Quaternion.Euler that runs the clock hands? If it's all in seconds, I can do the math for degrees of rotation, but implementing time is the trouble. Here's DateTime.Now....

    using System;
    using UnityEngine;

    public class ClockAnimator : MonoBehaviour {

    private const float
    hoursToDegrees = 360f / 12f,
    minutesToDegrees = 360f / 60f,
    secondsToDegrees = 360f / 60;

    public Transform hours, minutes, seconds;

    void Update() {
    DateTime time = DateTime.Now;
    hours.localRotation = Quaternion.Euler(0f, 0f, time.Hour * -hoursToDegrees);
    minutes.localRotation = Quaternion.Euler(0f, 0f, time.Minute * -minutesToDegrees);
    seconds.localRotation = Quaternion.Euler(0f, 0f, time.Second * -secondsToDegrees);
    }
    }
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712